-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmesh_morph.c
1564 lines (1339 loc) · 56.9 KB
/
mesh_morph.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
morphing of mesh coordinates with neighbor information
FORMAT: [c, d] = mesh_morph(c, n, tri, opts)
Input fields:
c Cx3 coordinate list
n Cx2 cell array of SRF xff object (1-based !)
tri Tx3 triangle list (1-based !)
opts mandatory struct with settings
.force 1x1 double smoothing force
.niter 1x1 double number of iterations
- optionally provided settings
.areac if 1x1 double := 1, keep area constant
(from initial state, requires .tri to be set!)
.areaw if given and [1], perform smoothing with area-based
weighting (default: false)
.distc if given and between 0 .. 1, perform distortion corr
.distw if given and [1], perform smoothing with distance-based
weighting (default: false)
.equalw apply equal weighting (assuming 6 neighbors, false)
.norm force along normal vector (default: 0)
.normramp if 1x1 double := 1, ramp along-normal force: 0 ... .norm
.sphere to-sphere force
.type 1xN char type, currently only 'smooth' supported
Output fields:
c morphed coordinates
d map values giving the triangle size/vertex after
morphing (optional, requires areac/tri to be set!) with
formula log(sqrt(TriangleSizeAtVertex / MeanTriangleSize));
Note: for smoothing the neighbor information is used, for distortion
correction the triangle data is used!
% Version: v0.9d
% Build: 14070113
% Date: Jul-01 2014, 1:37 PM EST
% Author: Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://neuroelf.net/
Copyright (c) 2010, 2011, 2014, Jochen Weber
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Columbia University nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mex.h"
#include "math.h"
#include <stdio.h>
#define MESH_PI 3.14159265358979323846
#define O_AREAMEAN 0
#define O_AREATOTAL 1
#define O_CENTEROFGRAVITY 2
#define O_DENSITYSMPDATA 3
#define O_NORMALVECTORS 4
#define O_ORIGAREAMEAN 5
#define O_ORIGAREATOTAL 6
#define O_ORIGDENSITYSMPDATA 7
#define O_RADIUSMEAN 8
#define O_XNUMBEROFFIELDS 9
double subFunc_area(int numc, int numt, const double *c, const unsigned long *t);
double subFunc_area_a(int numc, int numt, const double *c, const unsigned long *t, double *ca);
double subFunc_area_c(int numc, int numt, const double *c, const unsigned long *t, double *ca, const unsigned char *cad);
void subFunc_center(int numc, const double *c, double *cx, double *cy, double *cz);
void subFunc_normals(int numc, int numt, const double *c, const unsigned long *t, double *nrm);
double subFunc_radius(int numc, const double *c, double *cr);
void subFunc_vertexnrareas(int numc, int numt, const unsigned long *t, unsigned char *cad);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* number of coordinates (including double + triple), counter */
int nc, nc2, nc3;
int cc = 0;
double dnc;
/* number of triangles (including triple) */
int tc, tc3;
/* coordinate pointer (and backup pointer) */
double *c, *c2, *c3, *bc, *bc2, *bc3;
/* coordinate backup pointer */
double *cb, *cb2, *cb3, *bcb, *bcb2, *bcb3;
/* neighbors/triangle list pointer */
unsigned long *n = NULL;
unsigned long *tn = NULL;
/* number of neighbors list pointer */
unsigned char *nn = NULL;
/* triangle list */
const double *tria = NULL;
unsigned long *tri = NULL;
/* vertex area list, original vertex area list and number of areas per vertex list */
double *ca = NULL;
double *oca = NULL;
unsigned char *cad = NULL;
/* smoothing force (with default value) */
double force = 0.07, normforce = 0.0, rampforce = 0.0, rampfrom = 0.0, selfforce;
/* distortion correction values */
double distc = 0.5;
bool distcs = 0;
/* distance weighting */
bool areaw = 0, distw = 0, distwl = 0, distwsq = 0, equalw = 0, norm = 0, normramp = 0;
/* neighbor area, mean area, smoothing values */
double na[36];
double *bna;
double ma, dsv, cav;
/* to-sphere force and radius values */
double tosph = 0.0, e1x, e1y, e1z, e2x, e2y, e2z, nsx, nsy, nsz, nx, ny, nz, vlength;
bool sphere = 0;
/* mean radius and radius correction, radius list */
double mr;
double *cr, *bcr;
/* number of iterations */
double niterd = 1.0;
int niter = 1;
int iterc;
/* morphtype (from options) */
char mtypec[11];
int mtype = 0;
/* number of dimensions and dim size */
int nd;
const int *di;
/* field number, field array and array pointer */
int fn;
mxArray *faout;
const mxArray *fa;
double *fapout;
const double *fap;
/* coordinate and smoothing values, number of neighbors (and counter)<s */
double cvx, cvy, cvz, svx, svy, svz, dnn;
unsigned char cnn, cnc;
/* number of neighbor list elements, element counter, neighbor list offset */
int nel, ec, no;
/* area constantness */
bool areac = 0;
double areav, nareav, tarea, ocx = 0.0, ocy = 0.0, ocz = 0.0, ncx, ncy, ncz, scale;
/* double triangle value and sides */
double tv;
/* compute output flag */
bool compdm = 0;
int compds[2] = {1, 1};
int compdmnumfields = O_XNUMBEROFFIELDS;
const char *compdmfields[] = {
"AreaMean",
"AreaTotal",
"CenterOfGravity",
"DensitySMPData",
"NormalVectors",
"OrigAreaMean",
"OrigAreaTotal",
"OrigDensitySMPData",
"RadiusMean"
};
/* variable output string */
char vstr[256]; /* debugging */
/* check number of in/out arguments */
if ((nrhs != 4) || (nlhs > 2))
mexErrMsgTxt("Bad number of input/output arguments.");
/* density map requested -> requires triangles */
if (nlhs > 1)
compdm = 1;
/* check input argument types */
if (!mxIsDouble(prhs[0]) || !mxIsCell(prhs[1]) || !mxIsDouble(prhs[2]) || !mxIsStruct(prhs[3]))
mexErrMsgTxt("Bad argument type. Must be (double, cell, struct).");
/* check coordinates argument ndims */
nd = mxGetNumberOfDimensions(prhs[0]);
if (nd != 2)
mexErrMsgTxt("Coordinates must be 2-D array.");
/* check coordinates argument size (and get number of coordinates from this) */
di = mxGetDimensions(prhs[0]);
nc = di[0];
nc2 = nc * 2;
nc3 = nc * 3;
dnc = (double) nc;
if (di[1] != 3)
mexErrMsgTxt("Coordinates must be Cx3 matrix.");
/* check neighbors argument ndims */
nd = mxGetNumberOfDimensions(prhs[1]);
if (nd != 2)
mexErrMsgTxt("Neighbors must be 2-D array");
/* check neighbors argument size */
di = mxGetDimensions(prhs[1]);
if (di[0] != nc || di[1] != 2)
mexErrMsgTxt("Neighbors must be a Cx2 matrix.");
/* check triangles argument ndims */
nd = mxGetNumberOfDimensions(prhs[2]);
if (nd != 2)
mexErrMsgTxt("Triangles must be 2-D array");
/* check triangles argument size */
di = mxGetDimensions(prhs[2]);
if (di[1] != 3)
mexErrMsgTxt("Triangles must be a Tx3 matrix.");
tc = di[0];
tc3 = tc * 3;
tria = (double *) mxGetPr(prhs[2]);
/* check options argument ndims */
nd = mxGetNumberOfDimensions(prhs[3]);
if (nd != 2)
mexErrMsgTxt("Options must be a 2-D struct array.");
/* check options argument size */
if (mxGetNumberOfElements(prhs[3]) != 1)
mexErrMsgTxt("Options must be a 1x1 struct array.");
/* check options argument force field */
fn = mxGetFieldNumber(prhs[3], "force");
if (fn < 0)
mexErrMsgTxt("Options does not contain force information.");
/* check force field type */
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (!mxIsDouble(fa) || (mxGetNumberOfElements(fa) != 1))
mexErrMsgTxt("Options force field must be a 1x1 double.");
/* get and check force value */
force = *((double *) mxGetPr(fa));
if (mxIsInf(force) || mxIsNaN(force) || force < -16.0 || force > 16.0 || force == 0)
mexErrMsgTxt("Options force field contains invalid force value.");
selfforce = 1.0 - force;
/* check options argument niter field */
fn = mxGetFieldNumber(prhs[3], "niter");
if (fn < 0)
mexErrMsgTxt("Options does not contain niter information.");
/* check niter field type */
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (!mxIsDouble(fa) || (mxGetNumberOfElements(fa) != 1))
mexErrMsgTxt("Options niter field must be a 1x1 double.");
/* get and check niter value */
niterd = *((double *) mxGetPr(fa));
if (mxIsInf(niterd) || mxIsNaN(niterd) || niterd < 0 || niterd > 50000)
mexErrMsgTxt("Options niter field contains invalid niter value.");
niter = (int) niterd;
/* check options argument type field */
fn = mxGetFieldNumber(prhs[3], "type");
if (fn >= 0) {
mtype = -1;
/* check type field type */
fa = mxGetFieldByNumber(prhs[3], 0, fn);
nd = mxGetNumberOfDimensions(fa);
di = mxGetDimensions(fa);
if (!mxIsChar(fa) || (nd != 2) || (di[0] != 1) || (di[1] < 1) || (di[1] > 10))
mexErrMsgTxt("Options type field must be a 1xC char array.");
/* get and check type */
mxGetString(fa, mtypec, 10);
switch (mtypec[0]) {
case 's':
case 'S':
mtype = 0;
break;
}
}
if (mtype == -1)
mexErrMsgTxt("Options type field contains invalid value.");
/* check for distance weighting */
fn = mxGetFieldNumber(prhs[3], "areac");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
fap = (double *) mxGetPr(fa);
if (fap[0] == 1)
areac = 1;
}
}
/* check for area-based weighting */
fn = mxGetFieldNumber(prhs[3], "areaw");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc == 1.0)
areaw = 1;
}
}
/* check for normal-force ramping */
fn = mxGetFieldNumber(prhs[3], "normramp");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc == 1.0)
normramp = 1;
}
}
/* check for distance-based weighting */
fn = mxGetFieldNumber(prhs[3], "distw");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc == 1.0)
distw = 1;
}
}
fn = mxGetFieldNumber(prhs[3], "distwl");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc == 1.0) {
distwl = 1;
if (distwl)
distw = 1;
}
}
}
fn = mxGetFieldNumber(prhs[3], "distwsq");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc == 1.0) {
distwsq = 1;
if (distwsq)
distw = 1;
}
}
}
/* check for equal-based weighting */
fn = mxGetFieldNumber(prhs[3], "equalw");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc == 1.0)
equalw = 1;
}
}
/* check for distortion correction */
fn = mxGetFieldNumber(prhs[3], "distc");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
distc = *((double *) mxGetPr(fa));
if (!mxIsInf(distc) && !mxIsNaN(distc) && distc > 0.0 && distc <= 6.0)
distcs = 1;
}
}
/* apply along-normal force */
fn = mxGetFieldNumber(prhs[3], "rampfrom");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
normforce = *((double *) mxGetPr(fa));
if (!mxIsInf(normforce) && !mxIsNaN(normforce) && normforce >= -1.0 && normforce <= 1.0 && normforce != 0.0)
rampfrom = normforce;
normforce = 0.0;
}
}
fn = mxGetFieldNumber(prhs[3], "norm");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
normforce = *((double *) mxGetPr(fa));
if (!mxIsInf(normforce) && !mxIsNaN(normforce) && normforce >= -1.0 && normforce <= 1.0 && normforce != 0.0)
norm = 1;
}
}
/* apply to-sphere force */
fn = mxGetFieldNumber(prhs[3], "sphere");
if (fn >= 0) {
fa = mxGetFieldByNumber(prhs[3], 0, fn);
if (mxIsDouble(fa) && (mxGetNumberOfElements(fa) == 1)) {
fap = (double *) mxGetPr(fa);
if (!mxIsInf(*fap) && !mxIsNaN(*fap) && *fap > 0.0 && *fap <= 1.0) {
tosph = *fap;
sphere = 1;
}
}
}
/* create coordinates backup array */
cb = (double *) mxCalloc(3 * nc, sizeof(double));
if (cb == NULL)
mexErrMsgTxt("Error allocating coordinate backup buffer.");
/* prepare neighbors list */
n = (unsigned long*) mxCalloc(36 * nc, sizeof(unsigned long));
if (n == NULL)
mexErrMsgTxt("Error allocating neighbors list in memory.");
nn = (unsigned char*) mxCalloc(nc, sizeof(unsigned char));
if (nn == NULL)
mexErrMsgTxt("Error allocating number of neighbors list in memory.");
/* create triangle lookup array */
tri = (unsigned long *) mxCalloc(tc3, sizeof(unsigned long));
if (tri == NULL)
mexErrMsgTxt("Unable to allocate triangle list memory.");
/* parse triangle data */
if (tria == NULL)
mexErrMsgTxt("Triangle data required but not given.");
for (cc = 0; cc < tc3; ++cc) {
tv = tria[cc];
if (mxIsInf(tv) || mxIsNaN(tv) || tv < 1.0 || tv > dnc)
mexErrMsgTxt("Invalid tri option (each triangle vertex must be between 1 .. C).");
tri[cc] = ((unsigned long) tv) - 1;
}
/* parse neighbors into list */
for (cc = 0; cc < nc; ++cc) {
fa = mxGetCell(prhs[1], nc + cc);
if ((fa == NULL) || !mxIsDouble(fa))
mexErrMsgTxt("Invalid neighbors argument (empty neighbor).");
nel = mxGetNumberOfElements(fa);
if (nel > 36)
mexErrMsgTxt("Invalid neighbors argument (vertex must not have more than 36 neighbors).");
nn[cc] = (unsigned char) nel;
fap = (double *) mxGetPr(fa);
no = 36 * cc;
for (ec = 0; ec < nel; ++ec) {
tv = fap[ec];
if (mxIsInf(tv) || mxIsInf(tv) || tv < 1.0 || tv > dnc)
mexErrMsgTxt("Invalid neighbors argument (each neighbor must be between 1 .. C).");
n[no + ec] = ((unsigned long) tv) - 1;
}
}
/* create output */
di = mxGetDimensions(prhs[0]);
plhs[0] = mxCreateNumericArray(2, di, mxDOUBLE_CLASS, mxREAL);
c = (double *) mxGetPr(plhs[0]);
/* create second argument as well */
if (compdm) {
plhs[1] = mxCreateStructArray(2, compds, compdmnumfields, compdmfields);
if (plhs[1] == NULL)
mexErrMsgTxt("Error creating second output argument.");
}
/* copy coordinates from source */
fap = (double *) mxGetPr(prhs[0]);
bc = c;
for (cc = 0; cc < nc3; ++cc)
*c++ = *fap++;
c = bc;
/* get original area per vertex and total area */
ca = (double *) mxCalloc(nc, sizeof(double));
if (ca == NULL)
mexErrMsgTxt("Error allocating memory for coordinate area array.");
cad = (unsigned char *) mxCalloc(nc, sizeof(unsigned char));
if (cad == NULL)
mexErrMsgTxt("Error allocating memory for number of areas per coordinate array.");
cr = (double *) mxCalloc(nc, sizeof(double));
if (cr == NULL)
mexErrMsgTxt("Error allocating memory for coordinate area array.");
oca = (double *) mxCalloc(nc, sizeof(double));
if (oca == NULL)
mexErrMsgTxt("Error allocating memory for coordinate area array.");
subFunc_vertexnrareas(nc, tc, tri, cad);
areav = subFunc_area(nc, tc, c, tri);
ma = areav / dnc;
if (compdm) {
compds[0] = 1;
compds[1] = 1;
faout = mxCreateNumericArray(2, compds, mxDOUBLE_CLASS, mxREAL);
if (faout == NULL)
mexErrMsgTxt("Error creating AreaMean field in memory.");
fapout = (double *) mxGetPr(faout);
if (fapout == NULL)
mexErrMsgTxt("Error getting double pointer to AreaMean field.");
*fapout = ma;
mxSetFieldByNumber(plhs[1], 0, O_AREAMEAN, faout);
faout = mxCreateNumericArray(2, compds, mxDOUBLE_CLASS, mxREAL);
if (faout == NULL)
mexErrMsgTxt("Error creating AreaTotal field in memory.");
fapout = (double *) mxGetPr(faout);
if (fapout == NULL)
mexErrMsgTxt("Error getting double pointer to AreaTotal field.");
*fapout = areav;
mxSetFieldByNumber(plhs[1], 0, O_AREATOTAL, faout);
}
/* make backup of pointers */
bc = c;
bc2 = &bc[nc];
bc3 = &bc[nc2];
bcb = cb;
bcb2 = &bcb[nc];
bcb3 = &bcb[nc2];
bcr = cr;
/* get mean */
if (areac || sphere || norm) {
/* get and subtract center */
subFunc_center(nc, bc, &ocx, &ocy, &ocz);
if (compdm) {
compds[0] = 1;
compds[1] = 3;
faout = mxCreateNumericArray(2, compds, mxDOUBLE_CLASS, mxREAL);
if (faout == NULL)
mexErrMsgTxt("Error creating CenterOfGravity field in memory.");
fapout = (double *) mxGetPr(faout);
if (fapout == NULL)
mexErrMsgTxt("Error getting double pointer to CenterOfGravity field.");
*fapout++ = ocx;
*fapout++ = ocy;
*fapout++ = ocz;
mxSetFieldByNumber(plhs[1], 0, O_CENTEROFGRAVITY, faout);
}
c = bc;
c2 = bc2;
c3 = bc3;
for (cc = 0; cc < nc; ++cc) {
*c++ -= ocx;
*c2++ -= ocy;
*c3++ -= ocz;
}
}
/* no rampimg of normal force */
if (!normramp)
rampforce = normforce;
/* get radius */
if (sphere) {
mr = subFunc_radius(nc, bc, bcr);
if (compdm) {
compds[0] = 1;
compds[1] = 1;
faout = mxCreateNumericArray(2, compds, mxDOUBLE_CLASS, mxREAL);
if (faout == NULL)
mexErrMsgTxt("Error creating RadiusMean field in memory.");
fapout = (double *) mxGetPr(faout);
if (fapout == NULL)
mexErrMsgTxt("Error getting double pointer to RadiusMean field.");
*fapout = mr;
mxSetFieldByNumber(plhs[1], 0, O_RADIUSMEAN, faout);
}
/* make "unit sphere like" vectors */
c = bc;
cr = bcr;
for (cc = 0; cc < nc3; ++cc)
*c++ /= mr;
}
/* further processing depends on morph type */
switch (mtype) {
/* smoothing */
case 0:
/* for equal weighting */
if ((equalw) && (!areaw) && (!distw))
force /= 6.0;
/* iterated morph */
for (iterc = 0; iterc < niter; ++iterc) {
/* check for NaNs */
if (mxIsNaN(*bc)) {
sprintf(vstr, "Coordinates turned to NaN at %d. iteration.", iterc + 1);
mexErrMsgTxt(vstr);
}
/* ramping of normal force */
if (norm && normramp)
rampforce = rampfrom + (normforce - rampfrom) * ((double) iterc / ((double) (niter - 1)));
/* to-sphere force */
if (sphere) {
/* subtract any center */
subFunc_center(nc, bc, &ncx, &ncy, &ncz);
c = bc;
c2 = bc2;
c3 = bc3;
for (cc = 0; cc < nc; ++cc) {
*c++ -= ncx;
*c2++ -= ncy;
*c3++ -= ncz;
}
/* pull vertices towards radius 1 */
c = bc;
c2 = bc2;
c3 = bc3;
for (cc = 0; cc < nc; ++cc) {
cvx = *c;
cvy = *c2;
cvz = *c3;
mr = sqrt(cvx * cvx + cvy * cvy + cvz * cvz);
scale = 1.0 - tosph * (mr - 1) / mr;
*c++ *= scale;
*c2++ *= scale;
*c3++ *= scale;
}
}
/* copy coordinates */
c = bc;
cb = bcb;
for (cc = 0; cc < nc3; ++cc)
*cb++ = *c++;
/* apply smoothing */
c = bc;
c2 = bc2;
c3 = bc3;
cb = bcb;
cb2 = bcb2;
cb3 = bcb3;
/* with area-based weighting */
if (areaw) {
/* get average triangle-area and area associated with each vertex */
tarea = subFunc_area_a(nc, tc, bc, tri, ca) / dnc;
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
dnn = (double) cnn;
tn = &n[36 * cc];
/* sum neighbor coordinates */
for (svx = svy = svz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
svx += bcb[*tn];
svy += bcb2[*tn];
svz += bcb3[*tn++];
}
/* subtract coordinate from mean and multiply by force */
cvx = *cb++;
cvy = *cb2++;
cvz = *cb3++;
scale = (ca[cc] / tarea);
if (scale > 2.0)
scale = 2.0;
scale *= force;
*c++ = cvx + (svx / dnn - cvx) * scale;
*c2++ = cvy + (svy / dnn - cvy) * scale;
*c3++ = cvz + (svz / dnn - cvz) * scale;
}
/* with distance-based weighting */
} else if (distw) {
/* square of distance */
if (distwsq) {
/* with normal force */
if (norm) {
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
tn = &n[36 * cc];
/* sum neighbor coordinates */
cvx = *cb;
cvy = *cb2;
cvz = *cb3;
e2x = bcb[tn[cnn - 1]] - cvx;
e2y = bcb2[tn[cnn - 1]] - cvy;
e2z = bcb3[tn[cnn - 1]] - cvz;
vlength = sqrt(e2x * e2x + e2y * e2y + e2z * e2z) + 2.3e-16;
e2x /= vlength;
e2y /= vlength;
e2z /= vlength;
for (scale = svx = svy = svz = nsx = nsy = nsz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
e1x = e2x;
e1y = e2y;
e1z = e2z;
ncx = bcb[*tn];
ncy = bcb2[*tn];
ncz = bcb3[*tn++];
e2x = ncx - cvx;
e2y = ncy - cvy;
e2z = ncz - cvz;
dnn = e2x * e2x + e2y * e2y + e2z * e2z;
vlength = sqrt(dnn) + 2.3e-16;
scale += dnn;
svx += dnn * ncx;
svy += dnn * ncy;
svz += dnn * ncz;
e2x /= vlength;
e2y /= vlength;
e2z /= vlength;
nx = e2y * e1z - e2z * e1y;
ny = e2z * e1x - e2x * e1z;
nz = e2x * e1y - e2y * e1x;
vlength = sqrt(nx * nx + ny * ny + nz * nz) + 2.3e-16;
nsx += nx / vlength;
nsy += ny / vlength;
nsz += nz / vlength;
}
dnn = (double) cnn;
nsx /= dnn;
nsy /= dnn;
nsz /= dnn;
vlength = rampforce / (sqrt(nsx * nsx + nsy * nsy + nsz * nsz) + 2.3e-16);
/* subtract coordinate from mean and multiply by force */
dnn = force / scale;
*c++ = selfforce * (*cb++) + dnn * svx + nsx * vlength;
*c2++ = selfforce * (*cb2++) + dnn * svy + nsy * vlength;
*c3++ = selfforce * (*cb3++) + dnn * svz + nsz * vlength;
}
/* without normal force */
} else {
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
tn = &n[36 * cc];
/* sum neighbor coordinates */
for (scale = svx = svy = svz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
cvx = bcb[*tn] - *cb;
cvy = bcb2[*tn] - *cb2;
cvz = bcb3[*tn] - *cb3;
dnn = cvx * cvx + cvy * cvy + cvz * cvz;
scale += dnn;
svx += dnn * bcb[*tn];
svy += dnn * bcb2[*tn];
svz += dnn * bcb3[*tn++];
}
/* subtract coordinate from mean and multiply by force */
dnn = force / scale;
*c++ = selfforce * (*cb++) + dnn * svx;
*c2++ = selfforce * (*cb2++) + dnn * svy;
*c3++ = selfforce * (*cb3++) + dnn * svz;
}
}
/* log of (1 + dist) */
} else if (distwl) {
/* with normal force */
if (norm) {
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
tn = &n[36 * cc];
/* sum neighbor coordinates */
cvx = *cb;
cvy = *cb2;
cvz = *cb3;
e2x = bcb[tn[cnn - 1]] - cvx;
e2y = bcb2[tn[cnn - 1]] - cvy;
e2z = bcb3[tn[cnn - 1]] - cvz;
vlength = sqrt(e2x * e2x + e2y * e2y + e2z * e2z) + 2.3e-16;
e2x /= vlength;
e2y /= vlength;
e2z /= vlength;
for (scale = svx = svy = svz = nsx = nsy = nsz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
e1x = e2x;
e1y = e2y;
e1z = e2z;
ncx = bcb[*tn];
ncy = bcb2[*tn];
ncz = bcb3[*tn++];
e2x = ncx - cvx;
e2y = ncy - cvy;
e2z = ncz - cvz;
dnn = sqrt(e2x * e2x + e2y * e2y + e2z * e2z);
vlength = dnn + 2.3e-16;
dnn = log(1.0 + dnn) + 2.3e-16;
scale += dnn;
svx += dnn * ncx;
svy += dnn * ncy;
svz += dnn * ncz;
e2x /= vlength;
e2y /= vlength;
e2z /= vlength;
nx = e2y * e1z - e2z * e1y;
ny = e2z * e1x - e2x * e1z;
nz = e2x * e1y - e2y * e1x;
vlength = sqrt(nx * nx + ny * ny + nz * nz) + 2.3e-16;
nsx += nx / vlength;
nsy += ny / vlength;
nsz += nz / vlength;
}
dnn = (double) cnn;
nsx /= dnn;
nsy /= dnn;
nsz /= dnn;
vlength = rampforce / (sqrt(nsx * nsx + nsy * nsy + nsz * nsz) + 2.3e-16);
/* subtract coordinate from mean and multiply by force */
dnn = force / scale;
*c++ = selfforce * (*cb++) + dnn * svx + nsx * vlength;
*c2++ = selfforce * (*cb2++) + dnn * svy + nsy * vlength;
*c3++ = selfforce * (*cb3++) + dnn * svz + nsz * vlength;
}
/* without normal force */
} else {
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
tn = &n[36 * cc];
/* sum neighbor coordinates */
for (scale = svx = svy = svz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
cvx = bcb[*tn] - *cb;
cvy = bcb2[*tn] - *cb2;
cvz = bcb3[*tn] - *cb3;
dnn = log(1.0 + sqrt(cvx * cvx + cvy * cvy + cvz * cvz)) + 2.3e-16;
scale += dnn;
svx += dnn * bcb[*tn];
svy += dnn * bcb2[*tn];
svz += dnn * bcb3[*tn++];
}
/* subtract coordinate from mean and multiply by force */
dnn = force / scale;
*c++ = selfforce * (*cb++) + dnn * svx;
*c2++ = selfforce * (*cb2++) + dnn * svy;
*c3++ = selfforce * (*cb3++) + dnn * svz;
}
}
/* regular distance weighting */
} else {
/* with normal force */
if (norm) {
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
tn = &n[36 * cc];
/* sum neighbor coordinates */
cvx = *cb;
cvy = *cb2;
cvz = *cb3;
e2x = bcb[tn[cnn - 1]] - cvx;
e2y = bcb2[tn[cnn - 1]] - cvy;
e2z = bcb3[tn[cnn - 1]] - cvz;
vlength = sqrt(e2x * e2x + e2y * e2y + e2z * e2z) + 2.3e-16;
e2x /= vlength;
e2y /= vlength;
e2z /= vlength;
for (scale = svx = svy = svz = nsx = nsy = nsz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
e1x = e2x;
e1y = e2y;
e1z = e2z;
ncx = bcb[*tn];
ncy = bcb2[*tn];
ncz = bcb3[*tn++];
e2x = ncx - cvx;
e2y = ncy - cvy;
e2z = ncz - cvz;
dnn = sqrt(e2x * e2x + e2y * e2y + e2z * e2z) + 2.3e-16;
scale += dnn;
svx += dnn * ncx;
svy += dnn * ncy;
svz += dnn * ncz;
e2x /= dnn;
e2y /= dnn;
e2z /= dnn;
nx = e2y * e1z - e2z * e1y;
ny = e2z * e1x - e2x * e1z;
nz = e2x * e1y - e2y * e1x;
vlength = sqrt(nx * nx + ny * ny + nz * nz) + 2.3e-16;
nsx += nx / vlength;
nsy += ny / vlength;
nsz += nz / vlength;
}
dnn = (double) cnn;
nsx /= dnn;
nsy /= dnn;
nsz /= dnn;
vlength = rampforce / (sqrt(nsx * nsx + nsy * nsy + nsz * nsz) + 2.3e-16);
/* subtract coordinate from mean and multiply by force */
dnn = force / scale;
*c++ = selfforce * (*cb++) + dnn * svx + nsx * vlength;
*c2++ = selfforce * (*cb2++) + dnn * svy + nsy * vlength;
*c3++ = selfforce * (*cb3++) + dnn * svz + nsz * vlength;
}
/* without normal force */
} else {
/* iterate over vertices */
for (cc = 0; cc < nc; ++cc) {
/* get number of neighbors and offset */
cnn = nn[cc];
tn = &n[36 * cc];
/* sum neighbor coordinates */
for (scale = svx = svy = svz = 0.0, cnc = 0; cnc < cnn; ++cnc) {
cvx = bcb[*tn] - *cb;
cvy = bcb2[*tn] - *cb2;
cvz = bcb3[*tn] - *cb3;
dnn = sqrt(cvx * cvx + cvy * cvy + cvz * cvz + 2.3e-16);
scale += dnn;
svx += dnn * bcb[*tn];
svy += dnn * bcb2[*tn];
svz += dnn * bcb3[*tn++];
}
/* subtract coordinate from mean and multiply by force */
dnn = force / scale;
*c++ = selfforce * (*cb++) + dnn * svx;
*c2++ = selfforce * (*cb2++) + dnn * svy;
*c3++ = selfforce * (*cb3++) + dnn * svz;
}
}
}
/* without equal weighting */
} else if (equalw) {