MorphoGraphX
Curvature.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef CURVATURE_H
12 #define CURVATURE_H
13 
14 #include <Config.hpp>
15 
16 #include <Geometry.hpp>
17 #include <Information.hpp>
18 
19 #include <CImg.h>
20 #include <math.h>
21 #include <string>
22 #include <vector>
23 
24 #define CIMat cimg_library::CImg<float>
25 #define CIMatL cimg_library::CImgList<float>
26 
27 namespace mgx {
28 namespace util {
29 #ifndef isnan
30 using std::isnan;
31 #endif
32 
34 public:
35  virtual ~CurvatureMeasure() {
36  }
37  virtual float measure(float ev1, float ev2) = 0;
38  virtual std::string get_type() = 0;
39  virtual CurvatureMeasure* copy() = 0;
40 };
41 
43 public:
44  float measure(float ev1, float ev2) {
45  return ev1 * ev2;
46  }
47  std::string get_type() {
48  return "gaussian";
49  }
50  virtual CurvatureMeasure* copy() {
51  return new GaussianCurvature();
52  }
53 };
54 
56 public:
57  float measure(float ev1, float ev2) {
58  return (ev1 * ev1) + (ev2 * ev2);
59  }
60  std::string get_type() {
61  return "sumsquare";
62  }
63  virtual CurvatureMeasure* copy() {
64  return new SumSquareCurvature();
65  }
66 };
67 
69 public:
70  float measure(float ev1, float ev2) {
71  return sqrt((ev1 * ev1) + (ev2 * ev2));
72  }
73  std::string get_type() {
74  return "rootsumsquare";
75  }
76  virtual CurvatureMeasure* copy() {
77  return new RootSumSquareCurvature();
78  }
79 };
80 
82 public:
83  float measure(float ev1, float ev2) {
84  return (ev1 + ev2) / 2;
85  }
86  std::string get_type() {
87  return "average";
88  }
89  virtual CurvatureMeasure* copy() {
90  return new AverageCurvature();
91  }
92 };
93 
95 public:
96  float measure(float ev1, float ev2) {
97  return std::min(fabs(ev1), fabs(ev2));
98  }
99  std::string get_type() {
100  return "minimal";
101  }
102  virtual CurvatureMeasure* copy() {
103  return new MinimalCurvature();
104  }
105 };
106 
108 public:
109  float measure(float ev1, float ev2) {
110  return std::max(fabs(ev1), fabs(ev2));
111  }
112  std::string get_type() {
113  return "maximal";
114  }
115  virtual CurvatureMeasure* copy() {
116  return new MaximalCurvature();
117  }
118 };
119 
121 public:
122  float measure(float ev1, float ev2) {
123  return ev1 / ev2;
124  }
125  std::string get_type() {
126  return "anisotropy";
127  }
128  virtual CurvatureMeasure* copy() {
129  return new AnisotropyCurvature();
130  }
131 };
132 
134 public:
135  float measure(float ev1, float ev2) {
136  return (ev1 * ev2 < 0 ? -1.0f : 1.0f) * (fabs(ev1) + fabs(ev2)) / 2.0f;
137  }
138  std::string get_type() {
139  return "signedaverageabs";
140  }
141  virtual CurvatureMeasure* copy() {
142  return new SignedAverageAbsCurvature();
143  }
144 };
145 
146 class Curvature {
147 public:
148  // default constructor
149  Curvature()
150  : W(2, 2)
151  , ev1(0)
152  , ev2(0)
153  {
154  cM = new AverageCurvature();
155  }
156 
157  // convenience constructor
158  Curvature(std::string s)
159  : W(2, 2)
160  , ev1(0)
161  , ev2(0)
162  {
163  if(s == "gaussian")
164  cM = new GaussianCurvature();
165  else if(s == "average")
166  cM = new AverageCurvature();
167  else if(s == "minimal")
168  cM = new MinimalCurvature();
169  else if(s == "maximal")
170  cM = new MaximalCurvature();
171  else if(s == "sumsquare")
172  cM = new SumSquareCurvature();
173  else if(s == "rootsumsquare")
174  cM = new RootSumSquareCurvature();
175  else if(s == "signedaverageabs")
176  cM = new SignedAverageAbsCurvature();
177  else if(s == "anisotropy")
178  cM = new AnisotropyCurvature();
179  else
180  cM = new AverageCurvature();
181  }
182 
183  Curvature(const Curvature& copy)
184  : W(copy.W)
185  , b1(copy.b1)
186  , b2(copy.b2)
187  , b3(copy.b3)
188  , ed1(copy.ed1)
189  , ed2(copy.ed2)
190  , ev1(copy.ev1)
191  , ev2(copy.ev2)
192  , cM(0)
193  {
194  if(copy.cM)
195  cM = copy.cM->copy();
196  }
197 
198  ~Curvature() {
199  delete cM;
200  }
201 
202  // update the curvature object, first entry of pos and norm must be the position, surface normal of the point of
203  // interest
204  void update(const std::vector<Point3f>& pos, const std::vector<Point3f>& nrml)
205  {
206 
207  try {
208  if(nrml.size() > 0 && nrml.size() == pos.size()) {
209 
210  // Calculate local Coordinate system
211  update_local_Basis(nrml[0]);
212 
213  // Set up Least Square Problem
214  int cnt = pos.size();
215  CIMat X = CIMat(7, 3 * (cnt - 1));
216  CIMat b = CIMat(1, 3 * (cnt - 1));
217  Point3f vpos = pos[0];
218  int id = 0;
219 
220  for(int i = 1; i < cnt; i++) {
221  // Transform into local coordinates
222  Point3f vw = pos[i] - vpos;
223  float w1 = vw * b1;
224  float w2 = vw * b2;
225  float w3 = vw * b3;
226 
227  // Transform into local coordinates
228  float n1, n2, n3;
229  n1 = nrml[i] * b1;
230  n2 = nrml[i] * b2;
231  n3 = nrml[i] * b3;
232 
233  X(0, id) = 0.5 * w1 * w1;
234  X(1, id) = w1 * w2;
235  X(2, id) = 0.5 * w2 * w2;
236  X(3, id) = w1 * w1 * w1;
237  X(4, id) = w1 * w1 * w2;
238  X(5, id) = w1 * w2 * w2;
239  X(6, id) = w2 * w2 * w2;
240  b(id) = w3;
241  id++;
242 
243  X(0, id) = w1;
244  X(1, id) = w2;
245  X(2, id) = 0;
246  X(3, id) = 3 * w1 * w1;
247  X(4, id) = 2 * w1 * w2;
248  X(5, id) = w2 * w2;
249  X(6, id) = 0;
250  b(id) = -n1 / n3;
251  id++;
252 
253  X(0, id) = 0;
254  X(1, id) = w1;
255  X(2, id) = w2;
256  X(3, id) = 0;
257  X(4, id) = w1 * w1;
258  X(5, id) = 2 * w1 * w2;
259  X(6, id) = 3 * w2 * w2;
260  b(id) = -n2 / n3;
261  id++;
262  }
263  CIMat Xt = X.get_transpose();
264 
265  CIMat Q = Xt * X;
266 
267  b = Xt * b;
268 
269  float det = Q.det();
270  if(!isnan(det) && !(det == 0)) {
271  // solve least-squares problem
272  b.solve(Q);
273 
274  // update Weingarten-matrix
275  W(0, 0) = b[0];
276  W(1, 0) = W(0, 1) = b[1];
277  W(1, 1) = b[2];
278 
279  // calculate eigenvalues and eigenvectors
280  CIMatL bL = W.get_symmetric_eigen();
281  ev1 = bL(0)(0, 0);
282  ev2 = bL(0)(1, 0);
283  ed1 = -bL(1)(0, 0) * b1 - bL(1)(0, 1) * b2;
284  ed2 = -bL(1)(1, 0) * b1 - bL(1)(1, 1) * b2;
285  if(fabs(ev1) < fabs(ev2)) {
286  using std::swap;
287  swap(ev1, ev2);
288  swap(ed1, ed2);
289  }
290  }
291  }
292  return;
293  }
294  catch(...) {
295  Information::out << "Error calculating curvature" << endl;
296  }
297  }
298 
299  // get principal directions of curvature
300  void get_eigenVectors(Point3f& evec1, Point3f& evec2)
301  {
302  evec1 = ed1;
303  evec2 = ed2;
304  return;
305  }
306 
307  // get principal directions of curvature
308  void get_eigenValues(float& eval1, float& eval2)
309  {
310  eval1 = ev1;
311  eval2 = ev2;
312  return;
313  }
314 
315  // calculate a single-valued measure of curvature (for example "gaussian")
316  float get_curvature() {
317  return cM->measure(ev1, ev2);
318  }
319 
320  // get the name of the curvature measure (for example "gaussian")
321  std::string get_type() {
322  return cM->get_type();
323  }
324 
325  // set the curvature measure
326  void set_Measure(CurvatureMeasure* _cM)
327  {
328  if(cM)
329  delete cM;
330  cM = _cM;
331  return;
332  }
333 
334 private:
335  // calculate local basis with b1,b2, in-plane and b3 out-of-plane basis vectors
336  void update_local_Basis(Point3f nrml)
337  {
338  nrml = nrml / nrml.norm();
339  do {
340  b1 = Point3f(cimg_library::cimg::rand(), cimg_library::cimg::rand(), cimg_library::cimg::rand());
341  b1 = b1 / b1.norm();
342  } while(fabs(b1 * nrml) == 1);
343  b1 = b1 - (b1 * nrml) * nrml;
344  b1 = b1 / b1.norm();
345  b2 = b1.cross(nrml);
346  b3 = nrml;
347 
348  return;
349  }
350 
351  // Weingarten Matrix
352  CIMat W;
353 
354  // Local Basis Vectors
355  Point3f b1;
356  Point3f b2;
357  Point3f b3;
358 
359  // Principal directions of curvature
360  Point3f ed1;
361  Point3f ed2;
362 
363  // Principal values of curvature
364  float ev1;
365  float ev2;
366 
367  CurvatureMeasure* cM;
368 };
369 } // namespace util
370 } // namespace mgx
371 #endif
Definition: Curvature.hpp:94
Definition: Curvature.hpp:107
Definition: Curvature.hpp:55
CU_HOST_DEVICE T norm() const
Euclidean norm of the vector.
Definition: Vector.hpp:532
Definition: Curvature.hpp:42
Definition: Curvature.hpp:146
Common definitions and utilities for all geometry algorithms This file is shared by cuda...
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Definition: Curvature.hpp:68
Definition: Curvature.hpp:120
Definition: Curvature.hpp:133
Definition: Curvature.hpp:33
Definition: Curvature.hpp:81