File: panel3d.C

package info (click to toggle)
battleball 2.0-13
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,016 kB
  • ctags: 3,097
  • sloc: cpp: 15,310; makefile: 48; csh: 34
file content (108 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download | duplicates (5)
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
// Copyright (c) 1997 Philip A. Hardin (pahardin@cs.utexas.edu)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License v2 or later.


#include "panel3d.h"



/*=========================================================================*/
void panel3d::SetNormalAndConvexity(const pt3d pts[]) {
  isConvex= true;

  if (ptNums.Num() >=3) {

    // compute normal
    // use Newell's formulation (Foley, Van Dam, Feiner, & Hughes, pp. 477)

    normal= pt3d(0,0,0);
    // Remember, ptNums[0] is the same as ptNums[ptNums.Num()-1],
    // so we have to be careful below:
    int j= ptNums.Num()-2;
    forii(ptNums.Num()-1) {
      const pt3d& pt1= pts[ptNums[j]];
      const pt3d& pt2= pts[ptNums[i]];
      normal.x += (pt1.y - pt2.y) *(pt1.z + pt2.z);
      normal.y += (pt1.z - pt2.z) *(pt1.x + pt2.x);
      normal.z += (pt1.x - pt2.x) *(pt1.y + pt2.y);
      j =i;
    }
    normal.Normalize();
    
    // Remember, ptNums[0] is the same as ptNums[ptNums.Num()-1],
    // so we have to be careful below:
    fori(ptNums.Num()-1) {
      j= (i+1) % (ptNums.Num()-1);  // index of second point number
      int k= (i+2) % (ptNums.Num()-1);  // index of third point number
      // compute the normal for this particular sequence of three points
      pt3d norm3= (pts[ptNums[j]]-pts[ptNums[i]]).CrossProd(
		   pts[ptNums[k]]-pts[ptNums[j]]);
      // if the normal for these 3 pts is in the opposite direction as
      // the official normal for the polygon, then either these 3 pts form
      // a concave bend, so we know that this polygon is _not_ convex!
      if (norm3.Dot(normal) <0)
	isConvex= false;
    }
  }
}


/*-------------------------------------------------------------------------*/
/* In:  srcPtNums = array of point numbers which define this panel,
                    terminated by a negative value
   Out: srcPtNums = points one element past the terminating negative value
   
   Reads an initial number that indicates if the panel is single-sided
   (zero) or double-sided (non-zero).  Then reads point numbers which define
   the panel until a negative point number is seen.  The number of pt nums
   should equal the number of points in the panel.
*/
void panel3d::Init(int *&srcPtNums, pt3d pts[])
{ if (ptNums.Num() >0) ptNums.Empty();
  
  doubleSided= *(srcPtNums++);
  while (*srcPtNums >=0)
    ptNums.Add(*srcPtNums++);
  ptNums.Add(ptNums[0]);
  srcPtNums++;  // advance past negative value
  SetNormalAndConvexity(pts);
}


/*-------------------------------------------------------------------------*/
// The region2d must have at least 2 sides, or else the panel3d will
// simply be empty.
panel3d::panel3d(const region2d& rgn, fastPts& pts,
		 const pt3d& offset, const ang3d& ang, const pt3d& scale,
		 bool isDoubleSided)
{ pt3d	pt;
  int	ptNum,firstPtNum;

  doubleSided= isDoubleSided;
  if (rgn.numSides >=2) {
    forii(rgn.numSides) {
      pt.x= rgn.sides[i].pt.x;
      pt.y= rgn.sides[i].pt.y;
      pt.z= 0;
      ptNum= pts.Add((pt*scale >> ang) +offset);
      if (i==0) firstPtNum= ptNum;
      ptNums.Add(ptNum);
    }
    ptNums.Add(firstPtNum);
    SetNormalAndConvexity(pts.Array());
  }
}


/*-------------------------------------------------------------------------*/
panel3d::panel3d(const panel3d& p, pt3d panelPts[], fastPts& pts,
		 const pt3d& offset, const ang3d& ang, const pt3d& scale) {
  doubleSided= p.doubleSided;
  forii(p.ptNums.Num())
  { pt3d *pt= &panelPts[p.ptNums[i]];
    int ptNum= pts.Add( ((*pt)*scale >> ang) +offset);
    ptNums.Add(ptNum);
  }
  SetNormalAndConvexity(pts.Array());
}