~ubuntu-branches/ubuntu/wily/ginkgocadx/wily-proposed

« back to all changes in this revision

Viewing changes to src/cadxcore/api/math/geometry3d.h

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2013-10-24 21:28:17 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20131024212817-ej1skb9og09d3ht6
Tags: 3.5.0.1137.31+dfsg-1
New upstream release [October 2013]

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 *  $Id: geometry3d.h $
5
5
 *  Ginkgo CADx Project
6
6
 *
7
 
 *  Copyright 2008-12 MetaEmotion S.L. All rights reserved.
 
7
 *  Copyright 2008-14 MetaEmotion S.L. All rights reserved.
8
8
 *  http://ginkgo-cadx.com
9
9
 *
10
10
 *  This file is licensed under LGPL v3 license.
11
11
 *  See License.txt for details
12
12
 *
13
13
 */
14
 
#ifndef GEOMETRY3D_H
 
14
#ifndef GEOMETRY3D_H
15
15
#define GEOMETRY3D_H
16
16
 
17
17
#if defined(max)
90
90
                                x = (TComp) valX;
91
91
                                y = (TComp) valY;
92
92
                                z = (TComp) valZ;
93
 
                                t = 1.0;
 
93
                                t = (TComp) 1.0;
94
94
                        }
95
95
 
96
96
                        template <class TipoComp> inline Vector3D(const TipoComp* const valor) : x(v[0]), y(v[1]), z(v[2]), t(v[3]) {
97
97
                                x = (TComp) valor[0];
98
98
                                y = (TComp) valor[1];
99
99
                                z = (TComp) valor[2];
100
 
                                t = 1.0;
 
100
                                t = (TComp) 1.0;
101
101
                        }
102
102
 
103
103
                        ~Vector3D() {
113
113
                                x = (TComp) valX;
114
114
                                y = (TComp) valY;
115
115
                                z = (TComp) valZ;
116
 
                                t = 1.0;
 
116
                                t = (TComp) 1.0;
117
117
                                return (*this);
118
118
                        }
119
119
 
122
122
                                x = (TComp) valor;
123
123
                                y = (TComp) valor;
124
124
                                z = (TComp) valor;
125
 
                                t = 1.0;
 
125
                                t = (TComp) 1.0;
126
126
                                return *this;
127
127
                        }
128
128
 
129
129
                        //----------------------------------------------
 
130
                        //-- Load and Store
 
131
                        //----------------------------------------------
 
132
 
 
133
                        /* Load from array (interpret ncomp components) */
 
134
                        template <class TipoComp> inline void Load(int ncomp, TipoComp* mem) {
 
135
                                for (int i = 0; i < 4; ++i) {
 
136
                                        if (i < ncomp) {
 
137
                                                v[i] = (TComp) mem[i];
 
138
                                        }
 
139
                                        else {
 
140
                                                if (v[i] == 3) {
 
141
                                                        v[i] = (TComp)0.0;
 
142
                                                }
 
143
                                                else {
 
144
                                                        v[i] = (TComp)1.0;
 
145
                                                }
 
146
                                        }
 
147
                                }
 
148
                        }
 
149
 
 
150
                        /* Store to array (interpret ncomp components) */
 
151
                        template <class TipoComp> inline void Store(int ncomp, TipoComp* mem) const {
 
152
                                for (int i = 0; i < 4; ++i) {
 
153
                                        if (i < ncomp) {
 
154
                                                mem[i] = (TipoComp) v[i];
 
155
                                        }
 
156
                                }
 
157
                        }
 
158
 
 
159
                        //----------------------------------------------
130
160
                        //-- Normas Vectoriales, Distancias y Producto Interior
131
161
                        //----------------------------------------------
132
162
 
191
221
                                return ((TReal)a.x * (TReal)b.x) + ((TReal)a.y * (TReal)b.y) + ((TReal)a.z * (TReal)b.z);
192
222
                        }
193
223
 
194
 
                        /* Devuelve el producto vectorial */
195
 
                        inline Vector3D ProductoVectorial(const Vector3D& b) const {
196
 
                                const Vector3D& a = *this;
 
224
                        /* Devuelve el producto vectorial
 
225
                                w =  u  x v = (u_y v_z - u_z v_y) i + (u_z v_x - u_x v_z) j + (u_x v_y - u_y v_x) k 
 
226
                                |  i   j   k  |
 
227
                                | u.x u.y u.z | = { (u.y*v.z - u.z*v.y), (u.z*v.x - u.x*v.z), (u.x*v.y - u.y*v.x) }
 
228
                                | v.x v.y v.z |
 
229
                        */
 
230
                        inline Vector3D ProductoVectorial(const Vector3D& v) const {
 
231
                                const Vector3D& u = *this;
197
232
                                return Vector3D(
198
 
                                        ((TReal)a.y * (TReal)b.z) - ((TReal)b.y * (TReal)a.z),
199
 
                                        ((TReal)a.x * (TReal)b.z) - ((TReal)b.x * (TReal)a.z),
200
 
                                        ((TReal)a.x * (TReal)b.y) - ((TReal)b.x * (TReal)a.y)
 
233
                                        ((TReal)u.y * (TReal)v.z) - ((TReal)u.z * (TReal)v.y),
 
234
                                        ((TReal)u.z * (TReal)v.x) - ((TReal)u.x * (TReal)v.z),
 
235
                                        ((TReal)u.x * (TReal)v.y) - ((TReal)u.y * (TReal)v.x)
201
236
                                        );
202
237
                        }
203
238
 
316
351
                        }
317
352
 
318
353
                        /* Devuelve el punto de la proyeccion ortogonal del punto sobre la recta definida por p0 y p1 */
319
 
                        inline Vector3D ProyeccionOrtogonalSobreRecta(const Vector3D& /*p0*/, const Vector3D& /*p1*/) const {
320
 
                                //return InterseccionEntreRectas(*this, *this + (p1-p0).VectorOrtogonal(), p0, p1);
321
 
                                return Vector3D();
 
354
                        inline Vector3D ProyeccionOrtogonalSobreRecta(const Vector3D& p0, const Vector3D& p1) const {
 
355
                                 typedef Vector3D V;
 
356
                                 V vdir = p1 - p0;
 
357
                                 V::TComp proj = ( (*this) - p0).ProductoEscalar(vdir);
 
358
                                 return p0 + (vdir * proj);
322
359
                        }
323
360
 
324
361
                        /* Devuelve la distancia del punto a la recta definida por p0 y p1 */
325
 
                        inline TReal DistanciaARecta(const Vector3D& /*p0*/, const Vector3D& /*p1*/) const {
326
 
                                //return (ProyeccionOrtogonalSobreRecta(p0, p1) - (*this)).Norma2();
327
 
                                //
328
 
                                return 0.0;
 
362
                        inline TReal DistanciaARecta(const Vector3D& p0, const Vector3D& p1) const {
 
363
                                return (ProyeccionOrtogonalSobreRecta(p0, p1) - (*this)).Norma2();
329
364
                        }
330
365
 
331
366
                        /* Devuelve el cuadrado de la distancia del punto a la recta definida por p0 y p1 */
401
436
                                x = std::max(x, val);
402
437
                                y = std::max(y, val);
403
438
                                z = std::max(z, val);
 
439
                                t = (TComp) 1.0;
404
440
                                return *this;
405
441
                        }
406
442
 
409
445
                                x = std::min(x, val);
410
446
                                y = std::min(y, val);
411
447
                                z = std::min(z, val);
 
448
                                t = (TComp) 1.0;
412
449
                                return *this;
413
450
                        }
414
451
 
427
464
                                x = std::max(x, valx);
428
465
                                y = std::max(y, valy);
429
466
                                z = std::max(z, valz);
 
467
                                t = (TComp) 1.0;
430
468
                                return *this;
431
469
                        }
432
470
 
435
473
                                x = std::min(x, valx);
436
474
                                y = std::min(y, valy);
437
475
                                z = std::min(z, valz);
 
476
                                t = (TComp) 1.0;
438
477
                                return *this;
439
478
                        }
440
479
 
463
502
                                x = std::max(x, o.x);
464
503
                                y = std::max(y, o.y);
465
504
                                z = std::max(x, o.z);
 
505
                                t = (TComp) 1.0;
466
506
                                return *this;
467
507
                        }
468
508
 
471
511
                                x = std::min(x, o.x);
472
512
                                y = std::min(y, o.y);
473
513
                                z = std::min(z, o.z);
 
514
                                t = (TComp) 1.0;
474
515
                                return *this;
475
516
                        }
476
517
 
507
548
                                x = std::ceil(x);
508
549
                                y = std::ceil(y);
509
550
                                z = std::ceil(z);
 
551
                                t = (TComp) 1.0;
510
552
                                return *this;
511
553
                        }
512
554
 
518
560
                                x = std::floor(x);
519
561
                                y = std::floor(y);
520
562
                                z = std::floor(z);
 
563
                                t = (TComp) 1.0;
521
564
                                return *this;
522
565
                        }
523
566
 
540
583
                                x = ValorRedondeado(x);
541
584
                                y = ValorRedondeado(y);
542
585
                                z = ValorRedondeado(z);
 
586
                                t = (TComp) 1.0;
543
587
                                return *this;
544
588
                        }
545
589
 
592
636
 
593
637
 
594
638
                        template <class TipoComp> inline Vector3D& operator=(const TipoComp* const vec) {
595
 
                                x = (TComp) vec[0];
596
 
                                y = (TComp) vec[1];
597
 
                                z = (TComp) vec[2];
598
 
                                t = (TComp) vec[3];
 
639
                                v[0] = (TComp) vec[0];
 
640
                                v[1] = (TComp) vec[1];
 
641
                                v[2] = (TComp) vec[2];
 
642
                                //v[3] = (TComp) vec[3];
 
643
                                v[3] = 1.0;
599
644
                                return *this;
600
645
                        }
601
646
 
615
660
                                x *= otro.x;
616
661
                                y *= otro.y;
617
662
                                z *= otro.z;
 
663
                                t = (TComp) 1.0;
618
664
                                return *this;
619
665
                        }
620
666
 
626
672
                                x *= (TComp) valor;
627
673
                                y *= (TComp) valor;
628
674
                                z *= (TComp) valor;
 
675
                                t = (TComp) 1.0;
629
676
                                return *this;
630
677
                        }
631
678
 
637
684
                                x /= v.x;
638
685
                                y /= v.y;
639
686
                                z /= v.z;
 
687
                                t = (TComp) 1.0;
640
688
                                return *this;
641
689
                        }
642
690
 
648
696
                                x /= v;
649
697
                                y /= v;
650
698
                                z /= v;
 
699
                                t = (TComp) 1.0;
651
700
                                return *this;
652
701
                        }
653
702
 
659
708
                                x -= otro.x;
660
709
                                y -= otro.y;
661
710
                                z -= otro.z;
 
711
                                t = (TComp) 1.0;
662
712
                                return *this;
663
713
                        }
664
714
 
666
716
                                return Vector3D(x - valor, y - valor, z - valor);
667
717
                        }
668
718
 
 
719
                        /** Unary minus **/
 
720
                        inline Vector3D operator-() const {
 
721
                                return Vector3D(-x, -y, -z);
 
722
                        }
 
723
 
669
724
                        inline Vector3D& operator-=(const TComp& valor) {
670
725
                                x -= valor;
671
726
                                y -= valor;
672
727
                                z -= valor;
 
728
                                t = (TComp) 1.0;
673
729
                                return *this;
674
730
                        }
675
731
 
681
737
                                x += otro.x;
682
738
                                y += otro.y;
683
739
                                z += otro.z;
 
740
                                t = (TComp) 1.0;
684
741
                                return *this;
685
742
                        }
686
743
 
693
750
                                x += (TComp) valor;
694
751
                                y += (TComp) valor;
695
752
                                z += (TComp) valor;
 
753
                                t = (TComp) 1.0;
696
754
                                return *this;
697
755
                        }
698
756
 
 
757
                        inline operator TComp *() {
 
758
                                return v;
 
759
                        }
 
760
 
 
761
                        inline operator const TComp *() const {
 
762
                                return v;
 
763
                        }
 
764
 
 
765
                        //----------------------------------------------
 
766
                        //-- Utils
 
767
                        //----------------------------------------------
 
768
 
 
769
                        inline bool InsideOpenedBoundingBox(const Vector3D& p0, const Vector3D& p1) const {
 
770
                                bool vx = false;
 
771
                                bool vy = false;
 
772
                                bool vz = false;
 
773
                                if (p0.x < p1.x) {
 
774
                                        vx = (p0.x < x && x < p1.x);
 
775
                                }
 
776
                                else {
 
777
                                        vx = (p1.x < x && x < p0.x);
 
778
                                }
 
779
 
 
780
                                if (p0.y < p1.y) {
 
781
                                        vy = (p0.y < y && y < p1.y);
 
782
                                }
 
783
                                else {
 
784
                                        vy = (p1.y < y && y < p0.y);
 
785
                                }
 
786
 
 
787
                                if (p0.z < p1.z) {
 
788
                                        vz = (p0.z < z && z < p1.z);
 
789
                                }
 
790
                                else {
 
791
                                        vz = (p1.z < z && z < p0.z);
 
792
                                }
 
793
 
 
794
                                return vx && vy && vz;
 
795
                        }
 
796
 
 
797
                        inline bool InsideClosedBoundingBox(const Vector3D& p0, const Vector3D& p1) const {
 
798
                                bool vx = false;
 
799
                                bool vy = false;
 
800
                                bool vz = false;
 
801
                                if (p0.x < p1.x) {
 
802
                                        vx = (p0.x <= x && x <= p1.x);
 
803
                                }
 
804
                                else {
 
805
                                        vx = (p1.x <= x && x <= p0.x);
 
806
                                }
 
807
 
 
808
                                if (p0.y < p1.y) {
 
809
                                        vy = (p0.y <= y && y <= p1.y);
 
810
                                }
 
811
                                else {
 
812
                                        vy = (p1.y <= y && y <= p0.y);
 
813
                                }
 
814
 
 
815
                                if (p0.z < p1.z) {
 
816
                                        vz = (p0.z <= z && z <= p1.z);
 
817
                                }
 
818
                                else {
 
819
                                        vz = (p1.z <= z && z <= p0.z);
 
820
                                }
 
821
 
 
822
                                return vx && vy && vz;
 
823
                        }
 
824
 
 
825
                        inline void InitBoundingBox(Vector3D& p0, Vector3D& p1) const {
 
826
                                p0 = p1 = *this;
 
827
                        }
 
828
 
 
829
                        inline void InitBoundingBox(Vector3D bBox[2]) const {
 
830
                                InitBoundingBox(bBox[0], bBox[1]);
 
831
                        }
 
832
 
 
833
                        inline void ExtendBoundingBox(Vector3D& p0, Vector3D& p1) const {
 
834
                                if (p0.x <= p1.x) {
 
835
                                        if (x < p0.x) {
 
836
                                                p0.x = x;
 
837
                                        }
 
838
                                        else if (x > p1.x) {
 
839
                                                p1.x = x;
 
840
                                        }
 
841
                                }
 
842
                                else {
 
843
                                        if (x < p1.x) {
 
844
                                                p1.x = x;
 
845
                                        }
 
846
                                        else if (x > p0.x) {
 
847
                                                p0.x = x;
 
848
                                        }
 
849
                                }
 
850
                                if (p0.y <= p1.y) {
 
851
                                        if (y < p0.y) {
 
852
                                                p0.y = y;
 
853
                                        }
 
854
                                        else if (y > p1.y) {
 
855
                                                p1.y = y;
 
856
                                        }
 
857
                                }
 
858
                                else {
 
859
                                        if (y < p1.y) {
 
860
                                                p1.y = y;
 
861
                                        }
 
862
                                        else if (y > p0.y) {
 
863
                                                p0.y = y;
 
864
                                        }
 
865
                                }
 
866
                                if (p0.z <= p1.z) {
 
867
                                        if (z < p0.z) {
 
868
                                                p0.z = z;
 
869
                                        }
 
870
                                        else if (z > p1.z) {
 
871
                                                p1.z = z;
 
872
                                        }
 
873
                                }
 
874
                                else {
 
875
                                        if (z < p1.z) {
 
876
                                                p1.z = z;
 
877
                                        }
 
878
                                        else if (z > p0.z) {
 
879
                                                p0.z = z;
 
880
                                        }
 
881
                                }
 
882
                        }
 
883
 
 
884
                        inline void ExtendBoundingBox(Vector3D bBox[2]) const {
 
885
                                ExtendBoundingBox(bBox[0], bBox[1]);
 
886
                        }
 
887
 
699
888
                        //----------------------------------------------
700
889
                        //-- Accesores
701
890
                        //----------------------------------------------
720
909
                        }
721
910
                };
722
911
 
 
912
                template <class TipoComp> inline Vector3D operator+(const TipoComp val, const Vector3D& o) {
 
913
                        return Vector3D( (Vector3D::TComp)val + o.x, (Vector3D::TComp)val + o.y, (Vector3D::TComp)val + o.z );
 
914
                }
 
915
 
 
916
                template <class TipoComp> inline Vector3D operator-(const TipoComp val, const Vector3D& o) {
 
917
                        return Vector3D( (Vector3D::TComp)val - o.x, (Vector3D::TComp)val - o.y, (Vector3D::TComp)val - o.z );
 
918
                }
 
919
 
 
920
                template <class TipoComp> inline Vector3D operator*(const TipoComp val, const Vector3D& o) {
 
921
                        return Vector3D( (Vector3D::TComp)val * o.x, (Vector3D::TComp)val * o.y, (Vector3D::TComp)val * o.z );
 
922
                }
 
923
 
 
924
                template <class TipoComp> inline Vector3D operator/(const TipoComp val, const Vector3D& o) {
 
925
                        return Vector3D( (Vector3D::TComp)val / o.x, (Vector3D::TComp)val / o.y, (Vector3D::TComp)val / o.z );
 
926
                }
 
927
 
723
928
                class  Matriz3x3 {
724
929
 
725
930
                public: