Blender  V2.93
uvproject.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <math.h>
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_camera_types.h"
26 #include "DNA_object_types.h"
27 
28 #include "BLI_math.h"
29 #include "BLI_uvproject.h"
30 
31 typedef struct ProjCameraInfo {
32  float camangle;
33  float camsize;
34  float xasp, yasp;
35  float shiftx, shifty;
36  float rotmat[4][4];
37  float caminv[4][4];
40 
41 void BLI_uvproject_from_camera(float target[2], float source[3], ProjCameraInfo *uci)
42 {
43  float pv4[4];
44 
45  copy_v3_v3(pv4, source);
46  pv4[3] = 1.0;
47 
48  /* rotmat is the object matrix in this case */
49  if (uci->do_rotmat) {
50  mul_m4_v4(uci->rotmat, pv4);
51  }
52 
53  /* caminv is the inverse camera matrix */
54  mul_m4_v4(uci->caminv, pv4);
55 
56  if (uci->do_pano) {
57  float angle = atan2f(pv4[0], -pv4[2]) / ((float)M_PI * 2.0f); /* angle around the camera */
58  if (uci->do_persp == false) {
59  target[0] = angle; /* no correct method here, just map to 0-1 */
60  target[1] = pv4[1] / uci->camsize;
61  }
62  else {
63  float vec2d[2]; /* 2D position from the camera */
64  vec2d[0] = pv4[0];
65  vec2d[1] = pv4[2];
66  target[0] = angle * ((float)M_PI / uci->camangle);
67  target[1] = pv4[1] / (len_v2(vec2d) * (uci->camsize * 2.0f));
68  }
69  }
70  else {
71  if (pv4[2] == 0.0f) {
72  pv4[2] = 0.00001f; /* don't allow div by 0 */
73  }
74 
75  if (uci->do_persp == false) {
76  target[0] = (pv4[0] / uci->camsize);
77  target[1] = (pv4[1] / uci->camsize);
78  }
79  else {
80  target[0] = (-pv4[0] * ((1.0f / uci->camsize) / pv4[2])) / 2.0f;
81  target[1] = (-pv4[1] * ((1.0f / uci->camsize) / pv4[2])) / 2.0f;
82  }
83  }
84 
85  target[0] *= uci->xasp;
86  target[1] *= uci->yasp;
87 
88  /* adds camera shift + 0.5 */
89  target[0] += uci->shiftx;
90  target[1] += uci->shifty;
91 }
92 
93 /* could rv3d->persmat */
94 void BLI_uvproject_from_view(float target[2],
95  float source[3],
96  float persmat[4][4],
97  float rotmat[4][4],
98  float winx,
99  float winy)
100 {
101  float pv4[4], x = 0.0, y = 0.0;
102 
103  copy_v3_v3(pv4, source);
104  pv4[3] = 1.0;
105 
106  /* rotmat is the object matrix in this case */
107  mul_m4_v4(rotmat, pv4);
108 
109  /* almost ED_view3d_project_short */
110  mul_m4_v4(persmat, pv4);
111  if (fabsf(pv4[3]) > 0.00001f) { /* avoid division by zero */
112  target[0] = winx / 2.0f + (winx / 2.0f) * pv4[0] / pv4[3];
113  target[1] = winy / 2.0f + (winy / 2.0f) * pv4[1] / pv4[3];
114  }
115  else {
116  /* scaling is lost but give a valid result */
117  target[0] = winx / 2.0f + (winx / 2.0f) * pv4[0];
118  target[1] = winy / 2.0f + (winy / 2.0f) * pv4[1];
119  }
120 
121  /* v3d->persmat seems to do this funky scaling */
122  if (winx > winy) {
123  y = (winx - winy) / 2.0f;
124  winy = winx;
125  }
126  else {
127  x = (winy - winx) / 2.0f;
128  winx = winy;
129  }
130 
131  target[0] = (x + target[0]) / winx;
132  target[1] = (y + target[1]) / winy;
133 }
134 
135 /* 'rotmat' can be `obedit->obmat` when uv project is used.
136  * 'winx' and 'winy' can be from `scene->r.xsch/ysch` */
137 ProjCameraInfo *BLI_uvproject_camera_info(Object *ob, float (*rotmat)[4], float winx, float winy)
138 {
139  ProjCameraInfo uci;
140  Camera *camera = ob->data;
141 
142  uci.do_pano = (camera->type == CAM_PANO);
143  uci.do_persp = (camera->type == CAM_PERSP);
144 
145  uci.camangle = focallength_to_fov(camera->lens, camera->sensor_x) / 2.0f;
146  uci.camsize = uci.do_persp ? tanf(uci.camangle) : camera->ortho_scale;
147 
148  /* account for scaled cameras */
149  copy_m4_m4(uci.caminv, ob->obmat);
150  normalize_m4(uci.caminv);
151 
152  if (invert_m4(uci.caminv)) {
153  ProjCameraInfo *uci_pt;
154 
155  /* normal projection */
156  if (rotmat) {
157  copy_m4_m4(uci.rotmat, rotmat);
158  uci.do_rotmat = true;
159  }
160  else {
161  uci.do_rotmat = false;
162  }
163 
164  /* also make aspect ratio adjustment factors */
165  if (winx > winy) {
166  uci.xasp = 1.0f;
167  uci.yasp = winx / winy;
168  }
169  else {
170  uci.xasp = winy / winx;
171  uci.yasp = 1.0f;
172  }
173 
174  /* include 0.5f here to move the UVs into the center */
175  uci.shiftx = 0.5f - (camera->shiftx * uci.xasp);
176  uci.shifty = 0.5f - (camera->shifty * uci.yasp);
177 
178  uci_pt = MEM_mallocN(sizeof(ProjCameraInfo), "ProjCameraInfo");
179  *uci_pt = uci;
180  return uci_pt;
181  }
182 
183  return NULL;
184 }
185 
186 void BLI_uvproject_from_view_ortho(float target[2], float source[3], const float rotmat[4][4])
187 {
188  float pv[3];
189 
190  mul_v3_m4v3(pv, rotmat, source);
191 
192  /* ortho projection */
193  target[0] = -pv[0];
194  target[1] = pv[2];
195 }
196 
197 void BLI_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y)
198 {
199  uci->xasp *= scale_x;
200  uci->yasp *= scale_y;
201 }
typedef float(TangentPoint)[2]
#define M_PI
Definition: BLI_math_base.h:38
bool invert_m4(float R[4][4])
Definition: math_matrix.c:1187
void mul_m4_v4(const float M[4][4], float r[4])
Definition: math_matrix.c:866
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
void normalize_m4(float R[4][4]) ATTR_NONNULL()
Definition: math_matrix.c:1952
float focallength_to_fov(float focal_length, float sensor)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float len_v2(const float a[2]) ATTR_WARN_UNUSED_RESULT
@ CAM_PERSP
@ CAM_PANO
Object is a sort of wrapper for general info.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
Read Guarded memory(de)allocation.
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
GREAL2 vec2d[2]
Double vector 2D.
#define tanf(x)
#define atan2f(x, y)
#define fabsf(x)
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
float shiftx
float sensor_x
float shifty
float ortho_scale
float obmat[4][4]
void * data
bool do_rotmat
Definition: uvproject.c:38
float shiftx
Definition: uvproject.c:35
float rotmat[4][4]
Definition: uvproject.c:36
float camsize
Definition: uvproject.c:33
float shifty
Definition: uvproject.c:35
float caminv[4][4]
Definition: uvproject.c:37
float camangle
Definition: uvproject.c:32
void BLI_uvproject_from_view(float target[2], float source[3], float persmat[4][4], float rotmat[4][4], float winx, float winy)
Definition: uvproject.c:94
void BLI_uvproject_from_camera(float target[2], float source[3], ProjCameraInfo *uci)
Definition: uvproject.c:41
void BLI_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y)
Definition: uvproject.c:197
struct ProjCameraInfo ProjCameraInfo
void BLI_uvproject_from_view_ortho(float target[2], float source[3], const float rotmat[4][4])
Definition: uvproject.c:186
ProjCameraInfo * BLI_uvproject_camera_info(Object *ob, float(*rotmat)[4], float winx, float winy)
Definition: uvproject.c:137