Blender  V2.93
quadric.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
35 #include "BLI_math.h"
36 #include "BLI_strict_flags.h"
37 
38 #include "BLI_quadric.h" /* own include */
39 
40 #define QUADRIC_FLT_TOT (sizeof(Quadric) / sizeof(double))
41 
42 void BLI_quadric_from_plane(Quadric *q, const double v[4])
43 {
44  q->a2 = v[0] * v[0];
45  q->b2 = v[1] * v[1];
46  q->c2 = v[2] * v[2];
47 
48  q->ab = v[0] * v[1];
49  q->ac = v[0] * v[2];
50  q->bc = v[1] * v[2];
51 
52  q->ad = v[0] * v[3];
53  q->bd = v[1] * v[3];
54  q->cd = v[2] * v[3];
55 
56  q->d2 = v[3] * v[3];
57 }
58 
59 #if 0 /* UNUSED */
60 
61 static void quadric_to_tensor_m3(const Quadric *q, double m[3][3])
62 {
63  m[0][0] = q->a2;
64  m[0][1] = q->ab;
65  m[0][2] = q->ac;
66 
67  m[1][0] = q->ab;
68  m[1][1] = q->b2;
69  m[1][2] = q->bc;
70 
71  m[2][0] = q->ac;
72  m[2][1] = q->bc;
73  m[2][2] = q->c2;
74 }
75 
76 #endif
77 
87 static bool quadric_to_tensor_m3_inverse(const Quadric *q, double m[3][3], double epsilon)
88 {
89  const double det = (q->a2 * (q->b2 * q->c2 - q->bc * q->bc) -
90  q->ab * (q->ab * q->c2 - q->ac * q->bc) +
91  q->ac * (q->ab * q->bc - q->ac * q->b2));
92 
93  if (fabs(det) > epsilon) {
94  const double invdet = 1.0 / det;
95 
96  m[0][0] = (q->b2 * q->c2 - q->bc * q->bc) * invdet;
97  m[1][0] = (q->bc * q->ac - q->ab * q->c2) * invdet;
98  m[2][0] = (q->ab * q->bc - q->b2 * q->ac) * invdet;
99 
100  m[0][1] = (q->ac * q->bc - q->ab * q->c2) * invdet;
101  m[1][1] = (q->a2 * q->c2 - q->ac * q->ac) * invdet;
102  m[2][1] = (q->ab * q->ac - q->a2 * q->bc) * invdet;
103 
104  m[0][2] = (q->ab * q->bc - q->ac * q->b2) * invdet;
105  m[1][2] = (q->ac * q->ab - q->a2 * q->bc) * invdet;
106  m[2][2] = (q->a2 * q->b2 - q->ab * q->ab) * invdet;
107 
108  return true;
109  }
110 
111  return false;
112 }
113 
114 void BLI_quadric_to_vector_v3(const Quadric *q, double v[3])
115 {
116  v[0] = q->ad;
117  v[1] = q->bd;
118  v[2] = q->cd;
119 }
120 
122 {
123  memset(q, 0, sizeof(*q));
124 }
125 
127 {
128  add_vn_vn_d((double *)a, (double *)b, QUADRIC_FLT_TOT);
129 }
130 
132 {
133  add_vn_vnvn_d((double *)r, (const double *)a, (const double *)b, QUADRIC_FLT_TOT);
134 }
135 
136 void BLI_quadric_mul(Quadric *a, const double scalar)
137 {
138  mul_vn_db((double *)a, QUADRIC_FLT_TOT, scalar);
139 }
140 
141 double BLI_quadric_evaluate(const Quadric *q, const double v[3])
142 {
143  const double v00 = v[0] * v[0], v01 = v[0] * v[1], v02 = v[0] * v[2];
144  const double v11 = v[1] * v[1], v12 = v[1] * v[2];
145  const double v22 = v[2] * v[2];
146  return ((q->a2 * v00) + (q->ab * 2 * v01) + (q->ac * 2 * v02) + (q->ad * 2 * v[0]) + /* a */
147  (q->b2 * v11) + (q->bc * 2 * v12) + (q->bd * 2 * v[1]) + /* b */
148  (q->c2 * v22) + (q->cd * 2 * v[2]) + /* c */
149  (q->d2) /* d */
150  );
151 }
152 
153 bool BLI_quadric_optimize(const Quadric *q, double v[3], const double epsilon)
154 {
155  double m[3][3];
156 
159  mul_m3_v3_db(m, v);
160  negate_v3_db(v);
161  return true;
162  }
163 
164  return false;
165 }
void mul_m3_v3_db(const double M[3][3], double r[3])
Definition: math_matrix.c:935
void add_vn_vn_d(double *array_tar, const double *array_src, const int size)
Definition: math_vector.c:1423
void mul_vn_db(double *array_tar, const int size, const double f)
Definition: math_vector.c:1447
void add_vn_vnvn_d(double *array_tar, const double *array_src_a, const double *array_src_b, const int size)
Definition: math_vector.c:1433
MINLINE void negate_v3_db(double r[3])
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
_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 GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
ATTR_WARN_UNUSED_RESULT const BMVert * v
static unsigned a[3]
Definition: RandGen.cpp:92
static double epsilon
void BLI_quadric_mul(Quadric *a, const double scalar)
Definition: quadric.c:136
bool BLI_quadric_optimize(const Quadric *q, double v[3], const double epsilon)
Definition: quadric.c:153
void BLI_quadric_from_plane(Quadric *q, const double v[4])
Definition: quadric.c:42
#define QUADRIC_FLT_TOT
Definition: quadric.c:40
void BLI_quadric_to_vector_v3(const Quadric *q, double v[3])
Definition: quadric.c:114
static bool quadric_to_tensor_m3_inverse(const Quadric *q, double m[3][3], double epsilon)
Definition: quadric.c:87
double BLI_quadric_evaluate(const Quadric *q, const double v[3])
Definition: quadric.c:141
void BLI_quadric_add_qu_qu(Quadric *a, const Quadric *b)
Definition: quadric.c:126
void BLI_quadric_clear(Quadric *q)
Definition: quadric.c:121
void BLI_quadric_add_qu_ququ(Quadric *r, const Quadric *a, const Quadric *b)
Definition: quadric.c:131
double cd
Definition: BLI_quadric.h:31
double d2
Definition: BLI_quadric.h:31
double bd
Definition: BLI_quadric.h:31
double c2
Definition: BLI_quadric.h:31
double bc
Definition: BLI_quadric.h:31
double ab
Definition: BLI_quadric.h:31
double ad
Definition: BLI_quadric.h:31
double ac
Definition: BLI_quadric.h:31
double a2
Definition: BLI_quadric.h:31
double b2
Definition: BLI_quadric.h:31
ccl_device_inline float2 fabs(const float2 &a)