0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / BRepMesh / BRepMesh_Triangle.hxx
1 // Created on: 1993-09-23
2 // Created by: Didier PIFFAULT
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _BRepMesh_Triangle_HeaderFile
18 #define _BRepMesh_Triangle_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Macro.hxx>
23
24 #include <BRepMesh_DegreeOfFreedom.hxx>
25
26
27 //! Light weighted structure representing triangle 
28 //! of mesh consisting of oriented links.
29 class BRepMesh_Triangle
30 {
31 public:
32
33   DEFINE_STANDARD_ALLOC
34
35   //! Default constructor.
36   BRepMesh_Triangle()
37     : myMovability  (BRepMesh_Free)
38   {
39     myEdges[0] = 0;
40     myEdges[1] = 0;
41     myEdges[2] = 0;
42     myOrientations[0] = Standard_False;
43     myOrientations[1] = Standard_False;
44     myOrientations[2] = Standard_False;
45   }
46
47   //! Constructor.
48   //! @param theEdges array of edges of triangle.
49   //! @param theOrientations array of edge's orientations.
50   //! @param theMovability movability of triangle.
51   BRepMesh_Triangle(
52     const Standard_Integer          (&theEdges)[3],
53     const Standard_Boolean          (&theOrientations)[3],
54     const BRepMesh_DegreeOfFreedom  theMovability)
55   {
56     Initialize(theEdges, theOrientations, theMovability);
57   }
58   
59   //! Initializes the triangle by the given parameters.
60   //! @param theEdges array of edges of triangle.
61   //! @param theOrientations array of edge's orientations.
62   //! @param theMovability movability of triangle.
63   inline void Initialize(
64     const Standard_Integer          (&theEdges)[3],
65     const Standard_Boolean          (&theOrientations)[3],
66     const BRepMesh_DegreeOfFreedom  theMovability)
67   {
68     memcpy(myEdges, theEdges, sizeof(theEdges));
69     memcpy(myOrientations, theOrientations, sizeof(theOrientations));
70     myMovability   = theMovability;
71   }
72   
73   //! Gets edges with orientations composing the triangle.
74   //! @param[out] theEdges array edges are stored to.
75   //! @param[out] theOrientations array orientations are stored to.
76   inline void Edges(Standard_Integer (&theEdges)[3],
77                     Standard_Boolean (&theOrientations)[3]) const
78   {
79     memcpy(theEdges, myEdges, sizeof(myEdges));
80     memcpy(theOrientations, myOrientations, sizeof(myOrientations));
81   }
82   
83   //! Returns movability of the triangle.
84   inline BRepMesh_DegreeOfFreedom Movability() const 
85   {
86     return myMovability;
87   }
88   
89   //! Sets movability of the triangle.
90   inline void SetMovability(const BRepMesh_DegreeOfFreedom theMovability)
91   {
92     myMovability = theMovability;
93   }
94
95   //! Computes a hash code for this triangle, in the range [1, theUpperBound]
96   //! @param theUpperBound the upper bound of the range a computing hash code must be within
97   //! @return a computed hash code, in the range [1, theUpperBound]
98   inline Standard_Integer HashCode (const Standard_Integer theUpperBound) const
99   {
100     return ::HashCode (myEdges[0] + myEdges[1] + myEdges[2], theUpperBound);
101   }
102
103   //! Checks for equality with another triangle.
104   //! @param theOther triangle to be checked against this one.
105   //! @return TRUE if equal, FALSE if not.
106   inline Standard_Boolean IsEqual(const BRepMesh_Triangle& theOther) const
107   {
108     if (myMovability == BRepMesh_Deleted || theOther.myMovability == BRepMesh_Deleted)
109       return Standard_False;
110
111     if (myEdges[0] == theOther.myEdges[0] &&
112         myEdges[1] == theOther.myEdges[1] &&
113         myEdges[2] == theOther.myEdges[2])
114     {
115       return Standard_True;
116     }
117
118     if (myEdges[0] == theOther.myEdges[1] &&
119         myEdges[1] == theOther.myEdges[2] &&
120         myEdges[2] == theOther.myEdges[0])
121     {
122       return Standard_True;
123     }
124
125     if (myEdges[0] == theOther.myEdges[2] &&
126         myEdges[1] == theOther.myEdges[0] &&
127         myEdges[2] == theOther.myEdges[1])
128     {
129       return Standard_True;
130     }
131
132     return Standard_False;
133   }
134   
135   //! Alias for IsEqual.
136   inline Standard_Boolean operator ==(const BRepMesh_Triangle& theOther) const
137   {
138     return IsEqual(theOther);
139   }
140
141   Standard_Integer          myEdges[3];
142   Standard_Boolean          myOrientations[3];
143   BRepMesh_DegreeOfFreedom  myMovability;
144 };
145
146 //! Computes a hash code for the given triangle, in the range [1, theUpperBound]
147 //! @param theTriangle the triangle which hash code is to be computed
148 //! @param theUpperBound the upper bound of the range a computing hash code must be within
149 //! @return a computed hash code, in the range [1, theUpperBound]
150 inline Standard_Integer HashCode (const BRepMesh_Triangle& theTriangle, const Standard_Integer theUpperBound)
151 {
152   return theTriangle.HashCode (theUpperBound);
153 }
154
155 #endif