summaryrefslogtreecommitdiff
path: root/src/GProp/GProp_PEquation.cxx
blob: 5dea78bc6e2ae7bcdf2abb377b3cd36b9ed29db9 (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
#include <GProp_PEquation.ixx>
#include <GProp_PrincipalProps.hxx>
#include <GProp_PGProps.hxx>

GProp_PEquation::GProp_PEquation(const TColgp_Array1OfPnt& Pnts, 
				       const Standard_Real Tol) 
{
  GProp_PGProps Pmat(Pnts);
  g = Pmat.CentreOfMass(); 
  Standard_Real Xg,Yg,Zg;
  g.Coord(Xg,Yg,Zg);
  GProp_PrincipalProps Pp = Pmat.PrincipalProperties();
  gp_Vec V1 = Pp.FirstAxisOfInertia();
  Standard_Real Xv1,Yv1,Zv1;
  V1.Coord(Xv1,Yv1,Zv1); 
  gp_Vec V2 = Pp.SecondAxisOfInertia(); 
  Standard_Real Xv2,Yv2,Zv2;
  V2.Coord(Xv2,Yv2,Zv2);
  gp_Vec V3 = Pp.ThirdAxisOfInertia(); 
  Standard_Real Xv3,Yv3,Zv3;
  V3.Coord(Xv3,Yv3,Zv3);
  Standard_Real D,X,Y,Z;
  Standard_Real Dmx1 = RealFirst();
  Standard_Real Dmn1 = RealLast();
  Standard_Real Dmx2 = RealFirst();
  Standard_Real Dmn2 = RealLast();
  Standard_Real Dmx3 = RealFirst();
  Standard_Real Dmn3 = RealLast();

  for (Standard_Integer i = Pnts.Lower(); i <= Pnts.Upper();i++){
    Pnts(i).Coord(X,Y,Z);
    D = (X-Xg)*Xv1 +(Y-Yg)*Yv1 + (Z-Zg)*Zv1;
    if (D > Dmx1) Dmx1 = D;
    if (D < Dmn1) Dmn1 = D;
    D = (X-Xg)*Xv2 +(Y-Yg)*Yv2 + (Z-Zg)*Zv2;
    if (D > Dmx2) Dmx2 = D;
    if (D < Dmn2) Dmn2 = D;
    D = (X-Xg)*Xv3 +(Y-Yg)*Yv3 + (Z-Zg)*Zv3;
    if (D > Dmx3) Dmx3 = D;
    if (D < Dmn3) Dmn3 = D;
  }
  Standard_Integer dimension= 3 ;
  Standard_Integer It = 0;
  if (Abs(Dmx1-Dmn1) <= Tol)  {
    dimension =dimension-1;
    It =1;
  }
  if (Abs(Dmx2-Dmn2) <= Tol)  {
    dimension =dimension-1;
    It =2*(It+1);
  }
  if (Abs(Dmx3-Dmn3) <= Tol)  {
    dimension =dimension-1;
    It = 3*(It+1);
  }
  switch (dimension)  {
  case 0:
    {
      type = GProp_Point;
      break;
    }
  case 1:
    {
      type = GProp_Line;
      if (It == 4) v1 = V3;
      else if (It == 6) v1 = V2;
      else v1 = V1; 
      break;
    }
  case 2:
    {
      type = GProp_Plane;
      if (It == 1) v1 = V1;
      else if (It == 2) v1 =V2;
      else v1 = V3;
      break;
    }
  case 3:
    {
      type = GProp_Space;
      g.SetXYZ(g.XYZ() + Dmn1*V1.XYZ() + Dmn2*V2.XYZ() + Dmn3*V3.XYZ());
      v1 = (Dmx1-Dmn1)*V1;      
      v2 = (Dmx2-Dmn2)*V2;
      v3 = (Dmx3-Dmn3)*V3;
      break;
    }
  }
}
Standard_Boolean  GProp_PEquation::IsPlanar() const {

  if (type == GProp_Plane) return Standard_True;
  else  return Standard_False;
}

Standard_Boolean  GProp_PEquation::IsLinear() const {

  if (type == GProp_Line) return Standard_True;
  else  return Standard_False;
}

Standard_Boolean  GProp_PEquation::IsPoint() const {

  if (type == GProp_Point) return Standard_True;
  else  return Standard_False;
}

Standard_Boolean GProp_PEquation::IsSpace() const {
  if (type == GProp_Space) return Standard_True;
  else  return Standard_False;
}

gp_Pln  GProp_PEquation::Plane() const {
  if (!IsPlanar()) Standard_NoSuchObject::Raise();
  return gp_Pln(g,v1);
}
gp_Lin  GProp_PEquation::Line() const {
  if (!IsLinear()) Standard_NoSuchObject::Raise();
  return gp_Lin(g,gp_Dir(v1));  
}

gp_Pnt  GProp_PEquation::Point() const {
  if (!IsPoint()) Standard_NoSuchObject::Raise();
  return g;
}

void GProp_PEquation::Box(gp_Pnt& P  , gp_Vec& V1,
			     gp_Vec& V2 , gp_Vec& V3) const {
  if (!IsSpace()) Standard_NoSuchObject::Raise();
  P = g;
  V1 = v1;
  V2 = v2;
  V3 = v3;
}