-
Notifications
You must be signed in to change notification settings - Fork 1
/
sop_scallop.cpp
1462 lines (1136 loc) · 41.5 KB
/
sop_scallop.cpp
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
/*************************************************************
* Copyright (c) 2012 by Egor N. Chashchin. All Rights Reserved. *
**************************************************************/
/*
* main.cpp - Scallop - system for generating and visualization of attraction areas
* of stochastic non-linear attractors
*
* Version: 0.99
* Authors: Egor N. Chashchin
* Contact: iqcook@gmail.com
*
*/
#ifdef LINUX
#define DLLEXPORT
#define SIZEOF_VOID_P 8
#else
#define DLLEXPORT __declspec(dllexport)
#endif
//#define MAKING_DSO
#define SESI_LITTLE_ENDIAN 1
// CRT
#include <sys/stat.h>
#include <limits.h>
#include <strstream>
#include <iostream>
using namespace std;
// H
#include <GU/GU_Detail.h>
#include <GU/GU_PrimVolume.h>
#include <OBJ/OBJ_Node.h>
#include <SOP/SOP_Node.h>
#include <PRM/PRM_Include.h>
#include <PRM/PRM_Template.h>
#include <PRM/PRM_SpareData.h>
#include <PRM/PRM_ChoiceList.h>
#include <OP/OP_Operator.h>
#include <OP/OP_OperatorTable.h>
#include <OP/OP_Caller.h>
#include <UT/UT_Vector3.h>
#include <UT/UT_Ramp.h>
#include <UT/UT_Interrupt.h>
#include <VEX/VEX_Error.h>
#include <CVEX/CVEX_Context.h>
#include <CVEX/CVEX_Value.h>
#include <SHOP/SHOP_Node.h>
//////////////////////////////////////////////////////////////////////////
int thecallbackfuncdiv(void *data, int index, float time,const PRM_Template *tplate);
int thecallbackfuncdata(void *data, int index, float time,const PRM_Template *tplate);
#define STR_PARM(str, name, idx, vi, t) { evalString(str, name, &ifdIndirect[idx], vi, (float)t); }
#define INT_PARM(name, idx, vi, t) { return evalInt(name, &ifdIndirect[idx], vi, t); }
#define FLT_PARM(name, idx, vi, t) { return evalFloat(name, &ifdIndirect[idx], vi, t); }
class SOP_Scallop :
public SOP_Node
{
public:
SOP_Scallop(OP_Network *net, const char *name, OP_Operator *entry) : SOP_Node(net,name,entry)
{
if (!ifdIndirect) ifdIndirect = allocIndirect(256);
};
virtual ~SOP_Scallop() {};
static OP_Node *creator(OP_Network *net, const char *name, OP_Operator *entry);
static PRM_Template templateList[];
void SaveDivMap(float time);
void SaveData(float time);
protected:
virtual OP_ERROR cookMySop (OP_Context &context);
static int* ifdIndirect;
};
int* SOP_Scallop::ifdIndirect = 0;
static PRM_Name bindnames[] =
{
PRM_Name("enabled#", "Enabled"),
PRM_Name("obj#", "OBJ Path"),
PRM_Name("model#", "Model"),
PRM_Name("weight#", "Weight"),
PRM_Name("color#", "Color"),
PRM_Name("power#", "Power"),
PRM_Name("vexcode#", "CVEX"),
PRM_Name("radius#", "Radius"),
PRM_Name("parameter#", "Parameter"),
PRM_Name("daemons", "Number of Daemons"),
PRM_Name(0)
};
static PRM_Name modelNames[] =
{
PRM_Name("linear", "Linear"),
PRM_Name("spherical", "Spherical"),
PRM_Name("polar", "Polar"),
PRM_Name("swirl", "Swirl"),
PRM_Name("trig", "Trigonometric"),
PRM_Name("vex", "VEX"),
PRM_Name(0)
};
static PRM_ChoiceList modelMenu(PRM_CHOICELIST_SINGLE, modelNames);
static PRM_Template theBindTemplates[] =
{
PRM_Template(PRM_TOGGLE, 1, &bindnames[0], PRMoneDefaults),
PRM_Template(PRM_STRING, PRM_TYPE_DYNAMIC_PATH, 1, &bindnames[1], 0, 0, 0, 0, &PRM_SpareData::objGeometryPath),
PRM_Template(PRM_ORD, PRM_Template::PRM_EXPORT_MAX, 1,&bindnames[2], 0, &modelMenu),
PRM_Template(PRM_FLT, 1, &bindnames[3],PRMoneDefaults),
PRM_Template(PRM_RGB, 3, &bindnames[4]),
PRM_Template(PRM_FLT, 1, &bindnames[5],PRMoneDefaults),
PRM_Template(PRM_STRING, PRM_TYPE_DYNAMIC_PATH,PRM_Template::PRM_EXPORT_MAX, 1,
&bindnames[6], 0, 0, 0, 0,&PRM_SpareData::shopCVEX),
PRM_Template(PRM_FLT, 1, &bindnames[7],PRMoneDefaults),
PRM_Template(PRM_FLT, 1, &bindnames[8],PRMzeroDefaults),
PRM_Template()
};
PRM_Name savePathName("path","Path");
PRM_Default savePathDef(0,"");
PRM_Name mapPathName("mappath","Map Path");
PRM_Default mapPathDef(0,"");
PRM_Name mapMedialName("mapmedial","Use medial Division");
PRM_Name mapDivName("mapdiv","Finite Division");
PRM_Name countName("count","Count");
PRM_Default countDef(1000,"");
PRM_Name degrName("degr","Display Degradation");
PRM_Default degrDef(5,"");
PRM_Name visibleName("showpts","Show Points");
PRM_Name visibleSizeName("ptssz","Point Size");
PRM_Name radiiName("trackradii","Track Radius");
PRM_Name radiiScaleName("radiiscale","Radii Scale");
PRM_Name biasName("bias","Parameter Bias");
PRM_Default biasDef(0.5f,"");
static PRM_Name btnSpoolDivBgeo("spooldivbgeo", "Save Division Map");
static PRM_Name nodeCount("nodecount","Division Node Count");
static PRM_Name btnSpoolData("spoolscdata", "Save Data");
// RAMP
static PRM_Name SpValue ("spvalue", "Point value");
static PRM_Name SpOffset ("spoffset", "Point offset");
static PRM_Name SpInterp ("spinterp", "Point interpolation");
static PRM_Name paramnetricColorName("parmcolor", "Parametric Color");
static PRM_Name Ramp ("ramp", "Color Ramp");
static PRM_Template RampTemplates[] = {
PRM_Template (PRM_FLT_J, 1, &SpOffset, PRMzeroDefaults),
PRM_Template (PRM_FLT_J, 1, &SpValue, PRMzeroDefaults),
PRM_Template (PRM_INT, 1, &SpInterp, PRMtwoDefaults),
PRM_Template (),
};
static PRM_Default switcherInfo[] = {
PRM_Default( 14, "Setup"),
PRM_Default( 1, "Daemons")
};
PRM_Template SOP_Scallop::templateList[]=
{
PRM_Template(PRM_SWITCHER_EXCLUSIVE,2, &PRMswitcherName, switcherInfo),
// SPOOL SETUP
PRM_Template(PRM_INT,1,&countName,&countDef),
PRM_Template(PRM_INT,1,°rName,°rDef),
PRM_Template(PRM_TOGGLE,1,&radiiName,PRMoneDefaults),
PRM_Template(PRM_FLT,1,&radiiScaleName,PRMoneDefaults),
PRM_Template(PRM_FLT,1,&biasName,&biasDef),
PRM_Template(PRM_TOGGLE,1,¶mnetricColorName,PRMzeroDefaults),
PRM_Template (PRM_MULTITYPE_RAMP_RGB, NULL, 1, &Ramp, PRMtwoDefaults),
PRM_Template(PRM_CALLBACK, 1, &btnSpoolData, NULL, NULL, NULL,thecallbackfuncdata),
PRM_Template(PRM_FILE,1,&savePathName,&savePathDef),
PRM_Template(PRM_CALLBACK, 1, &btnSpoolDivBgeo, NULL, NULL, NULL,thecallbackfuncdiv),
PRM_Template(PRM_INT,1,&nodeCount,PRM100Defaults),
PRM_Template(PRM_FILE,1,&mapPathName,&mapPathDef),
PRM_Template(PRM_TOGGLE,1,&mapMedialName,PRMzeroDefaults),
PRM_Template(PRM_INT,1,&mapDivName, PRMtenDefaults),
// DAEMONS
PRM_Template(PRM_MULTITYPE_LIST,
theBindTemplates, 9, &bindnames[9],
PRMzeroDefaults, 0, &PRM_SpareData::multiStartOffsetOne),
// RECENT
PRM_Template(PRM_TOGGLE,1,&visibleName,PRMoneDefaults),
PRM_Template(PRM_INT,1,&visibleSizeName,PRMtwoDefaults),
PRM_Template()
};
OP_Node *SOP_Scallop::creator(OP_Network *net,const char *name,OP_Operator *entry)
{
return new SOP_Scallop(net, name, entry);
};
struct Methods
{
static void Linear(float*) {};
static void Spherical(float*_P)
{
float Len = sqrt(_P[0]*_P[0]+_P[1]*_P[1]+_P[2]*_P[2]);
if(Len!=0) { _P[0] /= Len; _P[1] /= Len; _P[2] /= Len+_P[1]; };
};
static void Polar(float*_P)
{
float Len = sqrt(_P[0]*_P[0]+_P[1]*_P[1]+_P[2]*_P[2]);
_P[0] = atan2(_P[0],_P[1]); _P[1] = Len;
};
static void Swirl(float*_P)
{
float Len = sqrt(_P[0]*_P[0]+_P[1]*_P[1]+_P[2]*_P[2]);
_P[0] = Len*cos(Len); _P[1] = Len*sin(Len);
};
static void Trigonometric(float*_P)
{
_P[2] = cos(abs(_P[2]+_P[0]))-abs(sin(_P[2]+_P[1]));
_P[0] = cos(_P[0]);
_P[1] = sin(_P[1]);
};
};
typedef void (*NonLinear)(float*);
struct Daemon
{
NonLinear method;
UT_DMatrix4 xform;
float c[3];
float weight;
float range[2];
float power;
UT_String vexsrc;
bool Transform(float w, UT_Vector3& P, float* C, float& R, float& p);
void SetupCVEX(UT_String script);
bool useVex;
CVEX_Context context;
float in[3];
float out[3];
float radius;
float parameter;
Daemon() : method(Methods::Linear), power(1.0f), useVex(false), radius(1.0f), parameter(0.0f) { range[0]=0; range[1]=0; };
~Daemon();
static float now;
static OP_Node* caller;
static float bias;
static bool useColor;
};
float Daemon::now=0.0f;
OP_Node* Daemon::caller=NULL;
float Daemon::bias=0.5f;
bool Daemon::useColor = true;
void Daemon::SetupCVEX(UT_String script)
{
vexsrc=script;
SHOP_Node *shop = caller->findSHOPNode(vexsrc);
caller->addExtraInput(shop, OP_INTEREST_DATA);
shop->buildVexCommand(vexsrc, shop->getSpareParmTemplates(), now);
shop->buildShaderString(script, now, 0);
UT_String op = "op:";
op += vexsrc;
vexsrc=op;
char *argv[4096];
int argc = vexsrc.parse(argv, 4096);
//OP_Caller C(caller);
//context.setOpCaller(&C);
//context.setTime(now);
context.addInput("P",CVEX_TYPE_VECTOR3,true);
context.load(argc, argv );
useVex = context.isLoaded();
if(useVex)
{
CVEX_Value* inv = context.findInput("P",CVEX_TYPE_VECTOR3);
inv->setData(in,1);
CVEX_Value* outv = context.findOutput("P",CVEX_TYPE_VECTOR3);
outv->setData(out,1);
};
};
bool Daemon::Transform(float w, UT_Vector3& P, float* C, float& R, float& p)
{
if(w<range[0]) return false;
if(w>=range[1]) return false;
UT_Vector3 _P=P;
_P = _P*xform;
if(useVex)
{
memcpy(in,_P.vec,12);
context.run(1,false);
memcpy(_P.vec,out,12);
}
else method(_P.vec);
if(useColor)
{
C[0]=C[0]+power*0.333*(c[0]-C[0]);
C[1]=C[1]+power*0.333*(c[1]-C[1]);
C[2]=C[2]+power*0.333*(c[2]-C[2]);
};
P = P+power*(_P-P);
R += power*(radius-R);
p += bias*(parameter-p);
return true;
};
Daemon::~Daemon() {};
OP_ERROR SOP_Scallop::cookMySop(OP_Context &context)
{
//OP_Node::flags().timeDep = 1;
bool clip = (lockInputs(context) < UT_ERROR_ABORT);
UT_BoundingBox bbox;
if(clip)
{
const GU_Detail* input = inputGeo(0,context);
if(input != NULL)
{
//UT_Matrix4 bm;
int res = input->getBBox(&bbox);
if(res == 0) clip = false;
}
else clip = false;
unlockInputs();
};
float now = context.getTime();
Daemon::now=now;
Daemon::caller=this;
Daemon::bias = evalFloat("bias",0,now);
UT_Ramp ramp;
float rampout[4];
bool useRamp = (evalInt("parmcolor",0,now)!=0);
if(useRamp)
{
//PRM_Template *rampTemplate = PRMgetRampTemplate ("ramp", PRM_MULTITYPE_RAMP_RGB, NULL, NULL);
if (ramp.getNodeCount () < 2)
{
ramp.addNode (0, UT_FRGBA (0, 0, 0, 1));
ramp.addNode (1, UT_FRGBA (1, 1, 1, 1));
};
updateRampFromMultiParm(now, getParm("ramp"), ramp);
};
gdp->clearAndDestroy();
bool showPts = (evalInt("showpts",0,now)!=0);
/*
if(showPts)
{
float sz = evalInt("ptssz",0,now);
if(sz > 0)
{
float one = 1.0f;
gdp->addAttribute("showpoints",4,GA_ATTRIB_FLOAT_&one);
gdp->addAttribute("revealsize",4,GB_ATTRIB_FLOAT,&sz);
};
};
*/
int cnt = evalInt("daemons", 0, now);
Daemon* daemons=new Daemon[cnt];
float weights = 0;
int totd=0;
for(int i=1;i<=cnt;i++)
{
bool skip = (evalIntInst("enabled#",&i,0,now)==0);
if(skip) continue;
Daemon& d = daemons[totd];
UT_String path = "";
evalStringInst("obj#", &i, path, 0, now);
if(path == "") continue;
SOP_Node* node = getSOPNode(path);
OBJ_Node* obj = dynamic_cast<OBJ_Node*>(node->getParent());
if(obj == NULL) continue;
addExtraInput(obj, OP_INTEREST_DATA);
//d.xform = obj->getWorldTransform(context); // 10.0
obj->getWorldTransform(d.xform, context);
d.weight = evalFloatInst("weight#",&i,0,now);
if(!useRamp)
{
d.c[0] = evalFloatInst("color#",&i,0,now);
d.c[1] = evalFloatInst("color#",&i,1,now);
d.c[2] = evalFloatInst("color#",&i,2,now);
};
int mth = evalIntInst("model#",&i,0,now);
switch(mth)
{
case 1:
d.method = Methods::Spherical;
break;
case 2:
d.method = Methods::Polar;
break;
case 3:
d.method = Methods::Swirl;
break;
case 4:
d.method = Methods::Trigonometric;
break;
case 5:
{
UT_String script;
evalStringInst("vexcode#", &i, script, 0, now);
d.SetupCVEX(script);
if(d.useVex)
{
OP_Node* shop = (OP_Node*)findSHOPNode(script);
addExtraInput(shop, OP_INTEREST_DATA);
}
break;
}
case 0:
default:
d.method = Methods::Linear;
};
d.power = evalFloatInst("power#",&i,0,now);
d.radius = evalFloatInst("radius#",&i,0,now);
d.parameter = evalFloatInst("parameter#",&i,0,now);
weights+=d.weight;
totd++;
};
if(totd == 0)
{
delete [] daemons;
return error();
}
float base = 0.0;
for(int i=0;i<totd;i++)
{
Daemon& d = daemons[i];
d.range[0]=base;
d.range[1] = base+d.weight/weights;
base=d.range[1];
};
int total = evalInt("count",0,now);
int degr = evalInt("degr",0,now);
total >>= degr;
GA_RWHandleI cntt(gdp->addIntTuple(GA_ATTRIB_POINT, "count", 4, GA_Defaults(1.0)));
GB_AttributeRef dt(gdp->addDiffuseAttribute(GEO_POINT_DICT));
gdp->addVariableName("Cd","Cd");
UT_Vector3 current(0,0,0);
float C[3] = { 0,0,0 };
float R=1.0f;
bool trackRadii = (evalInt("trackradii",0,now)!=0);
float rScale = evalFloat("radiiscale",0,now);
GB_AttributeRef rt;
if(trackRadii)
{
float one=1.0f;
rt = gdp->addPointAttrib("width",4,GB_ATTRIB_FLOAT,&one);
if(!GBisAttributeRefValid(rt)) trackRadii=false;
else gdp->addVariableName("width","WIDTH");
};
float zero=0.0f;
GB_AttributeRef pt = gdp->addPointAttrib("parameter",4,GB_ATTRIB_FLOAT,&zero);
if(GBisAttributeRefValid(pt)) gdp->addVariableName("parameter","PARAMETER");
float param=0.0f;
srand(0);
UT_Interrupt* boss = UTgetInterrupt();
boss->opStart("Computing...");
for(int i=-50;i<total;i++)
{
bool ok = false;
if (boss->opInterrupt()) break;
float w = double(rand())/double(RAND_MAX);
for(int j=0;j<totd;j++)
{
ok = daemons[j].Transform(w,current,C,R,param);
if(ok) break;
};
if(i<0) continue;
if(clip)
{
if(!bbox.isInside(current)) continue;
};
if(ok)
{
GEO_Point* p = gdp->appendPoint();
p->setPos(current);
float* Cd=p->castAttribData<float>(dt);
if(useRamp)
{
ramp.rampLookup(param,C);
}
memcpy(Cd,C,12);
if(trackRadii)
{
float* _R = p->castAttribData<float>(rt);
*_R=rScale*R;
};
if(GBisAttributeRefValid(pt))
{
float* _p = p->castAttribData<float>(pt);
*_p=param;
}
};
};
boss->opEnd();
delete [] daemons;
return error();
};
static float data[4];
static float* G = data+3;
int thecallbackfuncdiv(void *data, int index, float time,const PRM_Template *tplate)
{
SOP_Scallop *me = (SOP_Scallop *) data;
me->SaveDivMap(time);
return 0;
};
#define QUANT 65536
struct PointList
{
float* data;
int limit;
int count;
int chsize;
void add(float*);
void clear();
float* operator[](int i);
PointList(int _limit)
: limit(_limit)
, count(0)
, data(NULL)
, chsize(0)
{};
~PointList() { if(data != NULL) delete [] data; };
};
void PointList::add(float* p)
{
if(count == limit) return;
if(count == chsize)
{
int dev = limit-chsize;
if(dev > QUANT) dev = QUANT;
float* ch = new float[3*(chsize+dev)];
memset(ch,0,12*(chsize+dev));
memcpy(ch,data,12*chsize);
chsize+=dev;
delete [] data;
data = ch;
};
float* d = data + count*3;
memcpy(d,p,12);
count++;
};
void PointList::clear()
{
delete [] data;
count = 0;
chsize = 0;
data = NULL;
};
float* PointList::operator[](int i)
{
if(i>=count) return NULL;
return data+3*i;
};
#define NODECOUNT 8
class BoundBox
{
public:
BoundBox() :
organized(false)
, splitted(false)
, b(NULL)
, p(limit)
, count(0)
{};
~BoundBox()
{
if(b != NULL) delete [] b;
};
static int limit;
static float radius;
static bool medial;
bool organized;
float bounds[6];
void Organize(float* B)
{
memcpy(bounds,B,24);
organized = true;
};
bool CheckPoint(float* P);
bool splitted;
BoundBox* b;
void Split();
void Centroid(float*);
void Build(GU_Detail& gdp);
void Collapse();
PointList p;
int count;
};
int BoundBox::limit = -1;
float BoundBox::radius = 0;
bool BoundBox::medial = false;
bool BoundBox::CheckPoint(float* P)
{
if(P[0]<bounds[0]) return false;
if(P[0]>=bounds[3]) return false;
if(P[1]<bounds[1]) return false;
if(P[1]>=bounds[4]) return false;
if(P[2]<bounds[2]) return false;
if(P[2]>=bounds[5]) return false;
p.limit = limit;
if(!splitted)
{
p.add(P);
count++;
if(count < limit) return true;
Split();
for(int i=0;i<count;i++)
{
float* _P = p[i];
for(int j=0;j<NODECOUNT;j++)
{
if(b[j].CheckPoint(_P)) break;
};
};
p.clear();
return true;
};
for(int i=0;i<NODECOUNT;i++)
{
if(b[i].CheckPoint(P)) break;
};
return true;
};
void BoundBox::Split()
{
float C[3];
Centroid(C);
b = new BoundBox[8];
{
float B0[6] = {bounds[0],bounds[1],bounds[2], C[0],C[1],C[2]};
b[0].Organize(B0);
}
{
float B1[6] = {C[0],bounds[1],bounds[2], bounds[3],C[1],C[2]};
b[1].Organize(B1);
}
{
float B2[6] = {bounds[0],C[1],bounds[2], C[0],bounds[4],C[2]};
b[2].Organize(B2);
}
{
float B3[6] = {bounds[0],bounds[1],C[2], C[0],C[1],bounds[5]};
b[3].Organize(B3);
}
{
float B4[6] = {bounds[0],C[1],C[2],C[0],bounds[4],bounds[5]};
b[4].Organize(B4);
}
{
float B5[6] = {C[0],bounds[1],C[2],bounds[3],C[1],bounds[5]};
b[5].Organize(B5);
}
{
float B6[6] = {C[0],C[1],bounds[2],bounds[3],bounds[4],C[2]};
b[6].Organize(B6);
}
{
float B7[6] = {C[0],C[1],C[2],bounds[3],bounds[4],bounds[5]};
b[7].Organize(B7);
}
// ...
splitted = true;
};
void BoundBox::Centroid(float* C)
{
// ...SIMPLEST IMPLEMENTATION
C[0]=0; C[1]=0; C[2]=0;
if(medial)
{
C[0]=bounds[0]+bounds[3]; C[0]/=2;
C[1]=bounds[1]+bounds[4]; C[1]/=2;
C[2]=bounds[2]+bounds[5]; C[2]/=2;
}
else
{
for(int i=0;i<count;i++)
{
float* P=p[i];
C[0]+=P[0];C[1]+=P[1];C[2]+=P[2];
}
C[0]/=count; C[1]/=count; C[2]/=count;
}
};
float func(const UT_Vector3&) { return 0.0f; }
void BoundBox::Build(GU_Detail& gdp)
{
if(!organized) return;
if(!splitted)
{
if(count < 1) return;
GB_AttributeRef at = gdp.addPrimAttrib("count",4,GB_ATTRIB_INT,&count);
GB_AttributeRef rt = gdp.addAttrib("radius",4,GB_ATTRIB_FLOAT,&radius);
// SETUP BBOX
Collapse();
UT_BoundingBox bbox(bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]);
int div[3] = {1,1,1};
GEO_Primitive* pl = GU_PrimVolume::buildFromFunction(&gdp,func,bbox,div[0],div[1],div[2]);
if(GBisAttributeRefValid(at))
{
int* ct = pl->castAttribData<int>(at);
*ct = count;
};
return;
};
for(int i=0;i<NODECOUNT;i++) b[i].Build(gdp);
};
void BoundBox::Collapse()
{
float B[6] = { 10000000,10000000,10000000,-10000000,-10000000,-10000000 };
for(int i=0;i<count;i++)
{
float* P = p[i];
if(P[0]<B[0]) B[0]=P[0];
if(P[0]>B[3]) B[3]=P[0];
if(P[1]<B[1]) B[1]=P[1];
if(P[1]>B[4]) B[4]=P[1];
if(P[2]<B[2]) B[2]=P[2];
if(P[2]>B[5]) B[5]=P[2];
};
memcpy(bounds,B,24);
};
struct OctreeBox
{
OctreeBox(int _l) : level(_l), C(NULL), filled(false), count(0), radius(0.0f) {};
bool Insert(float*P);
~OctreeBox();
UT_BoundingBox bbox;
void Build(GU_Detail& gdp);
static GB_AttributeRef at, rt, it, bt;
static UT_BoundingBox parentbbox;
static int maxlevel;
private:
OctreeBox() : C(NULL), filled(false), count(0), radius(0.0f) {};
int level;
OctreeBox* C;
bool filled;
int count;
float radius;
void Split();
};
GB_AttributeRef OctreeBox::at;
GB_AttributeRef OctreeBox::rt;
GB_AttributeRef OctreeBox::it;
GB_AttributeRef OctreeBox::bt;
int OctreeBox::maxlevel = 1;
UT_BoundingBox OctreeBox::parentbbox;
OctreeBox::~OctreeBox()
{
if(C!=NULL) delete [] C;
};
bool OctreeBox::Insert(float*P)
{
UT_Vector3 p(P);
if(!bbox.isInside(p)) return false;
filled = true;
if(level != 0)
{
if(C == NULL) Split();
for(int i=0;i<8;i++)
{
if(C[i].Insert(P)) break;
};
}
else
{
if(P[3] > radius) radius = P[3];
count++;
};
return true;
};
void OctreeBox::Split()
{
C = new OctreeBox[8];
for(int i=0;i<8;i++)
{
C[i].level=level-1;
};
UT_Vector3 c = bbox.center();
UT_Vector3 m = bbox.minvec();
UT_Vector3 M = bbox.maxvec();
C[0].bbox = UT_BoundingBox(m.vec[0],m.vec[1],m.vec[2],c.vec[0],c.vec[1],c.vec[2]);
C[1].bbox = UT_BoundingBox(c.vec[0],m.vec[1],m.vec[2],M.vec[0],c.vec[1],c.vec[2]);
C[2].bbox = UT_BoundingBox(m.vec[0],c.vec[1],m.vec[2],c.vec[0],M.vec[1],c.vec[2]);
C[3].bbox = UT_BoundingBox(m.vec[0],m.vec[1],c.vec[2],c.vec[0],c.vec[1],M.vec[2]);
C[4].bbox = UT_BoundingBox(m.vec[0],c.vec[1],c.vec[2],c.vec[0],M.vec[1],M.vec[2]);
C[5].bbox = UT_BoundingBox(c.vec[0],m.vec[1],c.vec[2],M.vec[0],c.vec[1],M.vec[2]);
C[6].bbox = UT_BoundingBox(c.vec[0],c.vec[1],m.vec[2],M.vec[0],M.vec[1],c.vec[2]);
C[7].bbox = UT_BoundingBox(c.vec[0],c.vec[1],c.vec[2],M.vec[0],M.vec[1],M.vec[2]);
};
void OctreeBox::Build(GU_Detail& gdp)
{
if(!filled) return;
if(level == 0)
{
//if(count < 1) return;
int div[3] = {1,1,1};
GEO_Primitive* pl = GU_PrimVolume::buildFromFunction(&gdp,func,bbox,div[0],div[1],div[2]);
if(GBisAttributeRefValid(at))
{
int* ct = pl->castAttribData<int>(at);
*ct = count;
};
if(GBisAttributeRefValid(rt))
{
float* ct = pl->castAttribData<float>(rt);
*ct = radius;
};
if(GBisAttributeRefValid(it))
{
int* ct = pl->castAttribData<int>(it);
ct[0] = float(bbox.centerX()-parentbbox.xmin())/parentbbox.sizeX()*maxlevel;
ct[1] = float(bbox.centerY()-parentbbox.ymin())/parentbbox.sizeY()*maxlevel;
ct[2] = float(bbox.centerZ()-parentbbox.zmin())/parentbbox.sizeZ()*maxlevel;
};
}
else
{
for(int i=0;i<8;i++) C[i].Build(gdp);
}
return;
};