aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/ubOde/ODEMeshWorker.cs
blob: 5465035c580380cb792ac7e2c44e040013dff964 (plain)
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
/*
 * AJLDuarte 2012
 */

using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using OpenSim.Framework;
using OpenSim.Region.PhysicsModules.SharedBase;
using OdeAPI;
using log4net;
using Nini.Config;
using OpenMetaverse;

namespace OpenSim.Region.PhysicsModule.ubOde
{
    public enum MeshState : byte
    {
        noNeed = 0,

        loadingAsset = 1,

        AssetOK = 0x0f,     // 00001111

        NeedMask = 0x30,    // 00110000
        needMesh = 0x10,    // 00010000
        needAsset = 0x20,   // 00100000

        FailMask = 0xC0,    // 11000000
        AssetFailed = 0x40, // 01000000
        MeshFailed = 0x80,   // 10000000

        MeshNoColide = FailMask | needAsset
    }

    public enum meshWorkerCmnds : byte
    {
        nop = 0,
        addnew,
        changefull,
        changesize,
        changeshapetype,
        getmesh,
    }

    public class ODEPhysRepData
    {
        public PhysicsActor actor;
        public PrimitiveBaseShape pbs;
        public IMesh mesh;

        public Vector3 size;
        public Vector3 OBB;
        public Vector3 OBBOffset;

        public float volume;

        public byte shapetype;
        public bool hasOBB;
        public bool hasMeshVolume;
        public bool isTooSmall;
        public MeshState meshState;
        public UUID? assetID;
        public meshWorkerCmnds comand;
    }

    public class ODEMeshWorker
    {
        private ILog m_log;
        private ODEScene m_scene;
        private IMesher m_mesher;

        public bool meshSculptedPrim = true;
        public float meshSculptLOD = 32;
        public float MeshSculptphysicalLOD = 32;
        public float MinSizeToMeshmerize = 0.1f;

        private OpenSim.Framework.BlockingQueue<ODEPhysRepData> workQueue = new OpenSim.Framework.BlockingQueue<ODEPhysRepData>();
        private bool m_running;

        private Thread m_thread;

        public ODEMeshWorker(ODEScene pScene, ILog pLog, IMesher pMesher, IConfig pConfig)
        {
            m_scene = pScene;
            m_log = pLog;
            m_mesher = pMesher;

            if (pConfig != null)
            {
                meshSculptedPrim = pConfig.GetBoolean("mesh_sculpted_prim", meshSculptedPrim);
                meshSculptLOD = pConfig.GetFloat("mesh_lod", meshSculptLOD);
                MinSizeToMeshmerize =  pConfig.GetFloat("mesh_min_size", MinSizeToMeshmerize);
                MeshSculptphysicalLOD = pConfig.GetFloat("mesh_physical_lod", MeshSculptphysicalLOD);
            }
            m_running = true;
            m_thread = new Thread(DoWork);
            m_thread.Name = "OdeMeshWorker";
            m_thread.Start();
        }

        private void DoWork()
        {
            m_mesher.ExpireFileCache();

            while(m_running)
            {
                 ODEPhysRepData nextRep = workQueue.Dequeue();
                if(!m_running)
                    return;
                if (nextRep == null)
                    continue;
                if (m_scene.haveActor(nextRep.actor))
                {
                    switch (nextRep.comand)
                    {
                        case meshWorkerCmnds.changefull:
                        case meshWorkerCmnds.changeshapetype:
                        case meshWorkerCmnds.changesize:
                            GetMesh(nextRep);
                            if (CreateActorPhysRep(nextRep) && m_scene.haveActor(nextRep.actor))
                                m_scene.AddChange(nextRep.actor, changes.PhysRepData, nextRep);
                            break;
                        case meshWorkerCmnds.getmesh:
                            DoRepDataGetMesh(nextRep);
                            break;
                    }
                }
            }
        }

        public void Stop()
        {
            try
            {
                m_thread.Abort();
                workQueue.Clear();
            }
            catch
            {
            }
        }

        public void ChangeActorPhysRep(PhysicsActor actor, PrimitiveBaseShape pbs,
                                        Vector3 size, byte shapetype)
        {
            ODEPhysRepData repData = new ODEPhysRepData();
            repData.actor = actor;
            repData.pbs = pbs;
            repData.size = size;
            repData.shapetype = shapetype;

            CheckMesh(repData);
            CalcVolumeData(repData);
            m_scene.AddChange(actor, changes.PhysRepData, repData);
            return;
        }

        public ODEPhysRepData NewActorPhysRep(PhysicsActor actor, PrimitiveBaseShape pbs,
                                        Vector3 size, byte shapetype)
        {
            ODEPhysRepData repData = new ODEPhysRepData();
            repData.actor = actor;
            repData.pbs = pbs;
            repData.size = size;
            repData.shapetype = shapetype;

            CheckMesh(repData);
            CalcVolumeData(repData);
            m_scene.AddChange(actor, changes.AddPhysRep, repData);
            return repData;
        }

        public void RequestMesh(ODEPhysRepData repData)
        {
            repData.mesh = null;

            if (repData.meshState == MeshState.needAsset)
            {
                PrimitiveBaseShape pbs = repData.pbs;

                // check if we got outdated

                if (!pbs.SculptEntry || pbs.SculptTexture == UUID.Zero)
                {
                    repData.meshState = MeshState.noNeed;
                    return;
                }

                repData.assetID = pbs.SculptTexture;
                repData.meshState = MeshState.loadingAsset;

                repData.comand = meshWorkerCmnds.getmesh;
                workQueue.Enqueue(repData);
            }
        }

        // creates and prepares a mesh to use and calls parameters estimation
        public bool CreateActorPhysRep(ODEPhysRepData repData)
        {
            IMesh mesh = repData.mesh;

            if (mesh != null)
            {
                IntPtr vertices, indices;
                int vertexCount, indexCount;
                int vertexStride, triStride;

                mesh.getVertexListAsPtrToFloatArray(out vertices, out vertexStride, out vertexCount);
                mesh.getIndexListAsPtrToIntArray(out indices, out triStride, out indexCount);

                if (vertexCount == 0 || indexCount == 0)
                {
                    m_log.WarnFormat("[PHYSICS]: Invalid mesh data on prim {0} mesh UUID {1}",
                        repData.actor.Name, repData.pbs.SculptTexture.ToString());
                    repData.meshState = MeshState.MeshFailed;
                    repData.hasOBB = false;
                    repData.mesh = null;
                    m_scene.mesher.ReleaseMesh(mesh);
                }
                else
                {
                    repData.OBBOffset = mesh.GetCentroid();
                    repData.OBB = mesh.GetOBB();
                    repData.hasOBB = true;
                    mesh.releaseSourceMeshData();
                }
            }
            CalcVolumeData(repData);
            return true;
        }

        public void AssetLoaded(ODEPhysRepData repData)
        {
            if (m_scene.haveActor(repData.actor))
            {
                if (needsMeshing(repData)) // no need for pbs now?
                {
                    repData.comand = meshWorkerCmnds.changefull;
                    workQueue.Enqueue(repData);
                }
            }
            else
                repData.pbs.SculptData = Utils.EmptyBytes;
        }

        public void DoRepDataGetMesh(ODEPhysRepData repData)
        {
            if (!repData.pbs.SculptEntry)
                return;

            if (repData.meshState != MeshState.loadingAsset)
                return;

            if (repData.assetID == null || repData.assetID == UUID.Zero)
                return;

            if (repData.assetID != repData.pbs.SculptTexture)
                return;

            // check if it is in cache
            GetMesh(repData);
            if (repData.meshState != MeshState.needAsset)
            {
                CreateActorPhysRep(repData);
                m_scene.AddChange(repData.actor, changes.PhysRepData, repData);
                return;
            }

            RequestAssetDelegate assetProvider = m_scene.RequestAssetMethod;
            if (assetProvider == null)
                return;
            ODEAssetRequest asr = new ODEAssetRequest(this, assetProvider, repData, m_log);
        }


        /// <summary>
        /// Routine to figure out if we need to mesh this prim with our mesher
        /// </summary>
        /// <param name="pbs"></param>
        /// <returns></returns>
        public bool needsMeshing(ODEPhysRepData repData)
        {
            PrimitiveBaseShape pbs = repData.pbs;
            // check sculpts or meshs

            Vector3 scale = pbs.Scale;
            if(scale.X <= MinSizeToMeshmerize &&
               scale.Y <= MinSizeToMeshmerize &&
               scale.Z <= MinSizeToMeshmerize)
            {
                repData.isTooSmall = true;
                return false;
            }

            if (pbs.SculptEntry)
            {
                if (meshSculptedPrim)
                    return true;

                if (pbs.SculptType == (byte)SculptType.Mesh) // always do meshs
                    return true;

                return false;
            }

            // convex shapes have no holes
            ushort profilehollow = pbs.ProfileHollow;
            if(repData.shapetype == 2)
                profilehollow = 0;

            // if it's a standard box or sphere with no cuts, hollows, twist or top shear, return false since ODE can use an internal representation for the prim

            if ((pbs.ProfileShape == ProfileShape.Square && pbs.PathCurve == (byte)Extrusion.Straight)
                    || (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1
                    && pbs.Scale.X == pbs.Scale.Y && pbs.Scale.Y == pbs.Scale.Z))
            {

                if (pbs.ProfileBegin == 0 && pbs.ProfileEnd == 0
                    && profilehollow == 0
                    && pbs.PathTwist == 0 && pbs.PathTwistBegin == 0
                    && pbs.PathBegin == 0 && pbs.PathEnd == 0
                    && pbs.PathTaperX == 0 && pbs.PathTaperY == 0
                    && pbs.PathScaleX == 100 && pbs.PathScaleY == 100
                    && pbs.PathShearX == 0 && pbs.PathShearY == 0)
                {
                    return false;
                }
            }

            //  following code doesn't give meshs to boxes and spheres ever
            // and it's odd..  so for now just return true if asked to force meshs
            // hopefully mesher will fail if doesn't suport so things still get basic boxes

            int iPropertiesNotSupportedDefault = 0;

            if (profilehollow != 0)
                iPropertiesNotSupportedDefault++;

            if ((pbs.PathBegin != 0) || pbs.PathEnd != 0)
                iPropertiesNotSupportedDefault++;

            if ((pbs.PathTwistBegin != 0) || (pbs.PathTwist != 0))
                iPropertiesNotSupportedDefault++;

            if ((pbs.ProfileBegin != 0) || pbs.ProfileEnd != 0)
                iPropertiesNotSupportedDefault++;

            if ((pbs.PathScaleX != 100) || (pbs.PathScaleY != 100))
                iPropertiesNotSupportedDefault++;

            if ((pbs.PathShearX != 0) || (pbs.PathShearY != 0))
                iPropertiesNotSupportedDefault++;

            if (pbs.ProfileShape == ProfileShape.Circle && pbs.PathCurve == (byte)Extrusion.Straight)
                iPropertiesNotSupportedDefault++;

            if (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1 && (pbs.Scale.X != pbs.Scale.Y || pbs.Scale.Y != pbs.Scale.Z || pbs.Scale.Z != pbs.Scale.X))
                iPropertiesNotSupportedDefault++;

            if (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1)
                iPropertiesNotSupportedDefault++;

            // test for torus
            if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.Square)
            {
                if (pbs.PathCurve == (byte)Extrusion.Curve1)
                {
                    iPropertiesNotSupportedDefault++;
                }
            }
            else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
            {
                if (pbs.PathCurve == (byte)Extrusion.Straight)
                {
                    iPropertiesNotSupportedDefault++;
                }

                // ProfileCurve seems to combine hole shape and profile curve so we need to only compare against the lower 3 bits
                else if (pbs.PathCurve == (byte)Extrusion.Curve1)
                {
                    iPropertiesNotSupportedDefault++;
                }
            }
            else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
            {
                if (pbs.PathCurve == (byte)Extrusion.Curve1 || pbs.PathCurve == (byte)Extrusion.Curve2)
                {
                    iPropertiesNotSupportedDefault++;
                }
            }
            else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
            {
                if (pbs.PathCurve == (byte)Extrusion.Straight)
                {
                    iPropertiesNotSupportedDefault++;
                }
                else if (pbs.PathCurve == (byte)Extrusion.Curve1)
                {
                    iPropertiesNotSupportedDefault++;
                }
            }

            if (iPropertiesNotSupportedDefault == 0)
            {
                return false;
            }
            return true;
        }

        // see if we need a mesh and if so if we have a cached one
        // called with a new repData
        public void CheckMesh(ODEPhysRepData repData)
        {
            PhysicsActor actor = repData.actor;
            PrimitiveBaseShape pbs = repData.pbs;

            if (!needsMeshing(repData))
            {
                repData.meshState = MeshState.noNeed;
                repData.hasOBB = false;
                return;
            }

            IMesh mesh = null;

            Vector3 size = repData.size;

            int clod = (int)LevelOfDetail.High;
            byte shapetype = repData.shapetype;
            bool convex = shapetype == 2;

            mesh = m_mesher.GetMesh(actor.Name, pbs, size, clod, true, convex);

            if (mesh == null)
            {
                if (pbs.SculptEntry)
                {
                    if (pbs.SculptTexture != null && pbs.SculptTexture != UUID.Zero)
                    {
                        repData.assetID = pbs.SculptTexture;
                        repData.meshState = MeshState.needAsset;
                    }
                    else
                        repData.meshState = MeshState.MeshFailed;

                    return;
                }
                else
                {
                    repData.meshState = MeshState.needMesh;
                    mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true);
                    if (mesh == null)
                    {
                        repData.meshState = MeshState.MeshFailed;
                        return;
                    }
                }
            }

            repData.meshState = MeshState.AssetOK;
            repData.mesh = mesh;
            repData.OBB = mesh.GetOBB();
            repData.OBBOffset = mesh.GetCentroid();
            repData.hasOBB = true;

            if (pbs.SculptEntry)
            {
                repData.assetID = pbs.SculptTexture;
            }

            pbs.SculptData = Utils.EmptyBytes;
            return ;
        }

        public void GetMesh(ODEPhysRepData repData)
        {
            PhysicsActor actor = repData.actor;

            PrimitiveBaseShape pbs = repData.pbs;

            repData.mesh = null;
            repData.hasOBB = false;

            if (!needsMeshing(repData))
            {
                repData.meshState = MeshState.noNeed;
                return;
            }

            if (repData.meshState == MeshState.MeshFailed)
                return;

            if (pbs.SculptEntry)
            {
                if (repData.meshState == MeshState.AssetFailed)
                {
                    if (pbs.SculptTexture == repData.assetID)
                        return;
                }
            }

            repData.meshState = MeshState.noNeed;

            IMesh mesh = null;
            Vector3 size = repData.size;
            byte shapetype = repData.shapetype;

            bool convex;
            int clod = (int)LevelOfDetail.High;
            if (shapetype == 0)
                convex = false;
            else
            {
                convex = true;
                if (pbs.SculptType != (byte)SculptType.Mesh)
                    clod = (int)LevelOfDetail.Low;
            }

            mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true);

            if (mesh == null)
            {
                if (pbs.SculptEntry)
                {
                    if (pbs.SculptTexture == UUID.Zero)
                        return;

                    repData.assetID = pbs.SculptTexture;

                    if (pbs.SculptData == null || pbs.SculptData.Length == 0)
                    {
                        repData.meshState = MeshState.needAsset;
                        return;
                    }
                }
            }

            repData.mesh = mesh;
            repData.pbs.SculptData = Utils.EmptyBytes;

            if (mesh == null)
            {
                if (pbs.SculptEntry)
                    repData.meshState = MeshState.AssetFailed;
                else
                    repData.meshState = MeshState.MeshFailed;

                return;
            }

            repData.meshState = MeshState.AssetOK;

            return;
        }

        private void CalculateBasicPrimVolume(ODEPhysRepData repData)
        {
            Vector3 _size = repData.size;

            float volume = _size.X * _size.Y * _size.Z; // default
            if(repData.isTooSmall)
            {
                repData.volume = volume;
                return;
            }

            PrimitiveBaseShape _pbs = repData.pbs;
            float tmp;

            float hollowAmount = (float)_pbs.ProfileHollow * 2.0e-5f;
            float hollowVolume = hollowAmount * hollowAmount;

            switch (_pbs.ProfileShape)
            {
                case ProfileShape.Square:
                    // default box

                    if (_pbs.PathCurve == (byte)Extrusion.Straight)
                    {
                        if (hollowAmount > 0.0)
                        {
                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Square:
                                case HollowShape.Same:
                                    break;

                                case HollowShape.Circle:

                                    hollowVolume *= 0.78539816339f;
                                    break;

                                case HollowShape.Triangle:

                                    hollowVolume *= (0.5f * .5f);
                                    break;

                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }
                    }

                    else if (_pbs.PathCurve == (byte)Extrusion.Curve1)
                    {
                        //a tube

                        volume *= 0.78539816339e-2f * (float)(200 - _pbs.PathScaleX);
                        tmp = 1.0f - 2.0e-2f * (float)(200 - _pbs.PathScaleY);
                        volume -= volume * tmp * tmp;

                        if (hollowAmount > 0.0)
                        {
                            hollowVolume *= hollowAmount;

                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Square:
                                case HollowShape.Same:
                                    break;

                                case HollowShape.Circle:
                                    hollowVolume *= 0.78539816339f;
                                    break;

                                case HollowShape.Triangle:
                                    hollowVolume *= 0.5f * 0.5f;
                                    break;
                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }
                    }

                    break;

                case ProfileShape.Circle:

                    if (_pbs.PathCurve == (byte)Extrusion.Straight)
                    {
                        volume *= 0.78539816339f; // elipse base

                        if (hollowAmount > 0.0)
                        {
                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Same:
                                case HollowShape.Circle:
                                    break;

                                case HollowShape.Square:
                                    hollowVolume *= 0.5f * 2.5984480504799f;
                                    break;

                                case HollowShape.Triangle:
                                    hollowVolume *= .5f * 1.27323954473516f;
                                    break;

                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }
                    }

                    else if (_pbs.PathCurve == (byte)Extrusion.Curve1)
                    {
                        volume *= 0.61685027506808491367715568749226e-2f * (float)(200 - _pbs.PathScaleX);
                        tmp = 1.0f - .02f * (float)(200 - _pbs.PathScaleY);
                        volume *= (1.0f - tmp * tmp);

                        if (hollowAmount > 0.0)
                        {

                            // calculate the hollow volume by it's shape compared to the prim shape
                            hollowVolume *= hollowAmount;

                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Same:
                                case HollowShape.Circle:
                                    break;

                                case HollowShape.Square:
                                    hollowVolume *= 0.5f * 2.5984480504799f;
                                    break;

                                case HollowShape.Triangle:
                                    hollowVolume *= .5f * 1.27323954473516f;
                                    break;

                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }
                    }
                    break;

                case ProfileShape.HalfCircle:
                    if (_pbs.PathCurve == (byte)Extrusion.Curve1)
                    {
                        volume *= 0.5236f;

                        if (hollowAmount > 0.0)
                        {
                            hollowVolume *= hollowAmount;

                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Circle:
                                case HollowShape.Triangle:  // diference in sl is minor and odd
                                case HollowShape.Same:
                                    break;

                                case HollowShape.Square:
                                    hollowVolume *= 0.909f;
                                    break;

                                //                                case HollowShape.Triangle:
                                //                                    hollowVolume *= .827f;
                                //                                    break;
                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }

                    }
                    break;

                case ProfileShape.EquilateralTriangle:

                    if (_pbs.PathCurve == (byte)Extrusion.Straight)
                    {
                        volume *= 0.32475953f;

                        if (hollowAmount > 0.0)
                        {

                            // calculate the hollow volume by it's shape compared to the prim shape
                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Same:
                                case HollowShape.Triangle:
                                    hollowVolume *= .25f;
                                    break;

                                case HollowShape.Square:
                                    hollowVolume *= 0.499849f * 3.07920140172638f;
                                    break;

                                case HollowShape.Circle:
                                    // Hollow shape is a perfect cyllinder in respect to the cube's scale
                                    // Cyllinder hollow volume calculation

                                    hollowVolume *= 0.1963495f * 3.07920140172638f;
                                    break;

                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }
                    }
                    else if (_pbs.PathCurve == (byte)Extrusion.Curve1)
                    {
                        volume *= 0.32475953f;
                        volume *= 0.01f * (float)(200 - _pbs.PathScaleX);
                        tmp = 1.0f - .02f * (float)(200 - _pbs.PathScaleY);
                        volume *= (1.0f - tmp * tmp);

                        if (hollowAmount > 0.0)
                        {

                            hollowVolume *= hollowAmount;

                            switch (_pbs.HollowShape)
                            {
                                case HollowShape.Same:
                                case HollowShape.Triangle:
                                    hollowVolume *= .25f;
                                    break;

                                case HollowShape.Square:
                                    hollowVolume *= 0.499849f * 3.07920140172638f;
                                    break;

                                case HollowShape.Circle:

                                    hollowVolume *= 0.1963495f * 3.07920140172638f;
                                    break;

                                default:
                                    hollowVolume = 0;
                                    break;
                            }
                            volume *= (1.0f - hollowVolume);
                        }
                    }
                    break;

                default:
                    break;
            }

            float taperX1;
            float taperY1;
            float taperX;
            float taperY;
            float pathBegin;
            float pathEnd;
            float profileBegin;
            float profileEnd;

            if (_pbs.PathCurve == (byte)Extrusion.Straight || _pbs.PathCurve == (byte)Extrusion.Flexible)
            {
                taperX1 = _pbs.PathScaleX * 0.01f;
                if (taperX1 > 1.0f)
                    taperX1 = 2.0f - taperX1;
                taperX = 1.0f - taperX1;

                taperY1 = _pbs.PathScaleY * 0.01f;
                if (taperY1 > 1.0f)
                    taperY1 = 2.0f - taperY1;
                taperY = 1.0f - taperY1;
            }
            else
            {
                taperX = _pbs.PathTaperX * 0.01f;
                if (taperX < 0.0f)
                    taperX = -taperX;
                taperX1 = 1.0f - taperX;

                taperY = _pbs.PathTaperY * 0.01f;
                if (taperY < 0.0f)
                    taperY = -taperY;
                taperY1 = 1.0f - taperY;
            }

            volume *= (taperX1 * taperY1 + 0.5f * (taperX1 * taperY + taperX * taperY1) + 0.3333333333f * taperX * taperY);

            pathBegin = (float)_pbs.PathBegin * 2.0e-5f;
            pathEnd = 1.0f - (float)_pbs.PathEnd * 2.0e-5f;
            volume *= (pathEnd - pathBegin);

            // this is crude aproximation
            profileBegin = (float)_pbs.ProfileBegin * 2.0e-5f;
            profileEnd = 1.0f - (float)_pbs.ProfileEnd * 2.0e-5f;
            volume *= (profileEnd - profileBegin);

            repData.volume = volume;
        }

        private void CalcVolumeData(ODEPhysRepData repData)
        {
            if (repData.hasOBB)
            {
                Vector3 OBB = repData.OBB;
            }
            else
            {
                Vector3 OBB = repData.size;
                OBB.X *= 0.5f;
                OBB.Y *= 0.5f;
                OBB.Z *= 0.5f;

                repData.OBB = OBB;
                repData.OBBOffset = Vector3.Zero;
            }

            CalculateBasicPrimVolume(repData);
        }
    }

    public class ODEAssetRequest
    {
        ODEMeshWorker m_worker;
        private ILog m_log;
        ODEPhysRepData repData;

        public ODEAssetRequest(ODEMeshWorker pWorker, RequestAssetDelegate provider,
            ODEPhysRepData pRepData, ILog plog)
        {
            m_worker = pWorker;
            m_log = plog;
            repData = pRepData;

            repData.meshState = MeshState.AssetFailed;
            if (provider == null)
                return;

            if (repData.assetID == null)
                return;

            UUID assetID = (UUID) repData.assetID;
            if (assetID == UUID.Zero)
                return;

            repData.meshState = MeshState.loadingAsset;
            provider(assetID, ODEassetReceived);
        }

        void ODEassetReceived(AssetBase asset)
        {
            repData.meshState = MeshState.AssetFailed;
            if (asset != null)
            {
                if (asset.Data != null && asset.Data.Length > 0)
                {
                    repData.meshState = MeshState.noNeed;

                    if (!repData.pbs.SculptEntry)
                        return;
                    if (repData.pbs.SculptTexture != repData.assetID)
                        return;

//                    repData.pbs.SculptData = new byte[asset.Data.Length];
//                    asset.Data.CopyTo(repData.pbs.SculptData,0);
                    repData.pbs.SculptData = asset.Data;
                    repData.meshState = MeshState.AssetOK;
                    m_worker.AssetLoaded(repData);
                }
                else
                    m_log.WarnFormat("[PHYSICS]: asset provider returned invalid mesh data for prim {0} asset UUID {1}.",
                        repData.actor.Name, asset.ID.ToString());
            }
            else
                m_log.WarnFormat("[PHYSICS]: asset provider returned null asset for mesh of prim {0}.",
                    repData.actor.Name);
        }
    }
}