~neomilium/heekscnc/heeks

« back to all changes in this revision

Viewing changes to src/GTri.h

  • Committer: Dan Heeks
  • Date: 2008-07-17 14:54:00 UTC
  • Revision ID: git-v1:5b3ee80cd3c0bee835afb056556f883a80f2b6e0
Initial adding of all the HeeksCNC files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// GTri.h
 
2
 
 
3
// triangle used for Anders's DropCutter code
 
4
// written by Dan Heeks starting on May 2nd 2008
 
5
 
 
6
class GTri{
 
7
public:
 
8
        double m_p[9]; // three points
 
9
        double m_n[3]; // normal, calculate this when loading stl file ( or creating from solid )
 
10
        double m_box[4]; // minx miny maxx maxy
 
11
 
 
12
        GTri(const double* x){memcpy(m_p, x, 9*sizeof(double)); calculate_box_and_normal();}
 
13
 
 
14
        void calculate_box_and_normal(){
 
15
                double v1[3] = {m_p[3] - m_p[0], m_p[4] - m_p[1], m_p[5] - m_p[2]};
 
16
                double v2[3] = {m_p[6] - m_p[0], m_p[7] - m_p[1], m_p[8] - m_p[2]};
 
17
 
 
18
                m_n[0] = v1[1] * v2[2] - v1[2] * v2[1];
 
19
                m_n[1] = v1[2] * v2[0] - v1[0] * v2[2];
 
20
                m_n[2] = v1[0] * v2[1] - v1[1] * v2[0];
 
21
 
 
22
                // normalise it
 
23
                double m = sqrt(m_n[0] * m_n[0] + m_n[1] * m_n[1] + m_n[2] * m_n[2]);
 
24
                if(m > 0.000000001)
 
25
                {
 
26
                        m_n[0] /= m;
 
27
                        m_n[1] /= m;
 
28
                        m_n[2] /= m;
 
29
                }
 
30
 
 
31
                m_box[0] = m_p[0];
 
32
                if(m_p[3] < m_box[0])m_box[0] = m_p[3];
 
33
                if(m_p[6] < m_box[0])m_box[0] = m_p[6];
 
34
                m_box[1] = m_p[1];
 
35
                if(m_p[4] < m_box[1])m_box[1] = m_p[4];
 
36
                if(m_p[7] < m_box[1])m_box[1] = m_p[7];
 
37
                m_box[2] = m_p[0];
 
38
                if(m_p[3] > m_box[2])m_box[2] = m_p[3];
 
39
                if(m_p[6] > m_box[2])m_box[2] = m_p[6];
 
40
                m_box[3] = m_p[1];
 
41
                if(m_p[4] > m_box[3])m_box[3] = m_p[4];
 
42
                if(m_p[7] > m_box[3])m_box[3] = m_p[7];
 
43
        }
 
44
 
 
45
        static bool box_in_box(double *this_box, double *box){
 
46
                if(this_box[0]<box[0]-heeksCAD->GetTolerance()){
 
47
                        // left of tri is left of box
 
48
                        if(this_box[2]<box[0]-heeksCAD->GetTolerance()){
 
49
                                // right of tri is left of box
 
50
                                return false;
 
51
                        }
 
52
                        else if(this_box[2]<box[2] + heeksCAD->GetTolerance()){
 
53
                                // right of tri is in box
 
54
                                if(this_box[1]<box[1]-heeksCAD->GetTolerance()){
 
55
                                        // bottom of tri is below box
 
56
                                        if(this_box[3]<box[1]-heeksCAD->GetTolerance()){
 
57
                                                // top of tri is below of box
 
58
                                                return false;
 
59
                                        }
 
60
                                        else{
 
61
                                                // top of tri is in box or above it
 
62
                                                return true;
 
63
                                        }
 
64
                                }
 
65
                                else if(this_box[1]<box[3]+heeksCAD->GetTolerance()){
 
66
                                        // bottom of tri is in box
 
67
                                        return true;
 
68
                                }
 
69
                                else{
 
70
                                        // bottom of tri is above box
 
71
                                        return false;
 
72
                                }
 
73
                        }
 
74
                        else{
 
75
                                // right of tri is right of box
 
76
                                if(this_box[1]>box[1]-heeksCAD->GetTolerance() && this_box[3]<box[3]+heeksCAD->GetTolerance()){
 
77
                                        // top and bottom within box
 
78
                                        return true;
 
79
                                }
 
80
                                else{
 
81
                                        return false;
 
82
                                }
 
83
                        }
 
84
                }
 
85
                else if(this_box[0]<box[2]+heeksCAD->GetTolerance()){
 
86
                        // left of tri is within box
 
87
                        if(this_box[1]<box[1]-heeksCAD->GetTolerance()){
 
88
                                // bottom of tri is below box
 
89
                                if(this_box[3]<box[1]-heeksCAD->GetTolerance()){
 
90
                                        // top of tri is below of box
 
91
                                        return false;
 
92
                                }
 
93
                                else{
 
94
                                        // top of tri is in box or above it
 
95
                                        return true;
 
96
                                }
 
97
                        }
 
98
                        else if(this_box[1]<box[3]+heeksCAD->GetTolerance()){
 
99
                                // bottom of tri is in box
 
100
                                return true;
 
101
                        }
 
102
                        else{
 
103
                                // bottom of tri is above box
 
104
                                return false;
 
105
                        }
 
106
                }
 
107
                else{
 
108
                        // left of tri is right of box
 
109
                        return false;
 
110
                }
 
111
        }
 
112
};
 
 
b'\\ No newline at end of file'