aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/games/jogl/impl/nurbs/internals/Arc.java
blob: 076c68b72ee6af5ee1cc81be6c164bd4b65bd81a (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
**
** http://oss.sgi.com/projects/FreeB
**
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
**
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
**
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
*/

/* an arc, in two list, the trim list and bin */
public class Arc {
  public static final int SIDE_NONE   = 0;
  public static final int SIDE_RIGHT  = 1;
  public static final int SIDE_TOP    = 2;
  public static final int SIDE_LEFT   = 3;
  public static final int SIDE_BOTTOM = 4;

  public static final int bezier_tag = (1 << 13);
  public static final int arc_tag    = (1 <<  3);
  public static final int tail_tag   = (1 <<  6);
  public Arc    prev;  /* trim list pointer */
  public Arc    next;  /* trim list pointer */
  public Arc    link;  /* bin pointers */
  public BezierArc bezierArc; /* associated bezier arc */
  public PwlArc    pwlArc;    /* associated pwl arc */
  public long      type;      /* curve type */
  public long      nuid;

  private static final float ZERO = 0.00001f;

  public Arc(Arc j, PwlArc p) {
    pwlArc = p;
    type = j.type;
    nuid = j.nuid;
  }

  public Arc(int arcSide, long nuid) {
    type = 0;
    setside(arcSide);
    this.nuid = nuid;
  }

  public Arc append(Arc jarc) {
    if ( jarc != null ) {
      next = jarc.next;
      prev = jarc;
      next.prev = prev.next = this;
    } else {
      next = prev = this;
    }
    return this;
  }

  public boolean check() {
    Arc jarc = this;
    do {
      assert( (jarc.pwlArc != null) || (jarc.bezierArc != null) );

      if (jarc.prev == 0 || jarc.next == 0) {
        System.out.println( "checkjarc:null next/prev pointer");
        jarc.print( );
        return false;
      }

      if (jarc.next.prev != jarc) {
        System.out.println( "checkjarc: pointer linkage screwed up");
        jarc.print( );
        return false;
      }

      if( jarc.pwlArc != null ) {
        assert( jarc.pwlArc.npts >= 1 );
        assert( jarc.pwlArc.npts < 100000 );
        if( jarc.prev.pwlArc != null ) {
          if( jarc.tail()[1] != jarc.prev.rhead()[1] ) {
            System.out.println( "checkjarc: geometric linkage screwed up 1");
            jarc.prev.show();
            jarc.show();
            return false;
          }
          if( jarc.tail()[0] != jarc.prev.rhead()[0] ) {
            System.out.println( "checkjarc: geometric linkage screwed up 2");
            jarc.prev.show();
            jarc.show();
            return false;
          }
        }
        if( jarc.next.pwlArc ) {
          if( jarc.next.tail()[0] != jarc.rhead()[0] ) {
            System.out.println( "checkjarc: geometric linkage screwed up 3");
            jarc.show();
            jarc.next.show();
            return false;
          }
          if( jarc.next.tail()[1] != jarc.rhead()[1] ) {
            System.out.println( "checkjarc: geometric linkage screwed up 4");
            jarc.show();
            jarc.next.show();
            return false;
          }
        }
        if( jarc.isbezier() ) {
          assert( jarc.pwlArc.npts == 2 );
          assert( (jarc.pwlArc.pts[0].param[0] ==
                   jarc.pwlArc.pts[1].param[0]) ||
                  (jarc.pwlArc.pts[0].param[1] ==
                   jarc.pwlArc.pts[1].param[1]) );
        }
      }
      jarc = jarc.next;
    } while (jarc != this);
    return true;
  }

  /**
   * Checks if tail of arc and head of prev meet.
   */
  public boolean isDisconnected() {
    if( pwlArc == 0 ) return 0;
    if( prev.pwlArc == 0 ) return 0;

    float[] p0 = tail();
    float[] p1 = prev.rhead();

    if( ((p0[0] - p1[0]) > ZERO) || ((p1[0] - p0[0]) > ZERO) ||
	((p0[1] - p1[1]) > ZERO) || ((p1[1] - p0[1]) > ZERO)  ) {
      return true;
    } else {
      /* average two points together */
      p0[0] = p1[0] = (p1[0] + p0[0]) * 0.5f;
      p0[1] = p1[1] = (p1[1] + p0[1]) * 0.5f;
      return false;
    }
  }

  /**
   * Counts number of points on arc loop.
   */
  public int numpts( ) {
    Arc jarc = this;
    int npts = 0;
    do {
	npts += jarc.pwlArc.npts;
	jarc = jarc.next;
    } while( jarc != this );
    return npts;
  }

  /**
   * Marks each point with id of arc.
   */
  public void markverts( void ) {
    Arc jarc = this;
	
    do {
	TrimVertex p = jarc.pwlArc.pts;
	for( int i=0; i<jarc.pwlArc.npts; i++ )
	    p[i].nuid = jarc.nuid;
	jarc = jarc.next;
    } while( jarc != this );
  }

  /**
   * Finds axis extrema on arc loop.
   */
  public void    getextrema( Arc[4] ) {
    float leftpt, botpt, rightpt, toppt;

    extrema[0] = extrema[1] = extrema[2] = extrema[3] = this;

    leftpt = rightpt = this.tail()[0];
    botpt  = toppt   = this.tail()[1];

    for( Arc jarc = this.next; jarc != this; jarc = jarc.next ) {
	if ( jarc.tail()[0] <  leftpt || 
	    (jarc.tail()[0] <= leftpt && jarc.rhead()[0]<=leftpt))  {
	    leftpt = jarc.pwlArc.pts.param[0];
	    extrema[1] = jarc;
	}
	if ( jarc.tail()[0] >  rightpt || 
	    (jarc.tail()[0] >= rightpt && jarc.rhead()[0] >= rightpt)) {
	    rightpt = jarc.pwlArc.pts.param[0];
	    extrema[3] = jarc;
	}
	if ( jarc.tail()[1] <  botpt || 
            (jarc.tail()[1] <= botpt && jarc.rhead()[1] <= botpt ))  {
	    botpt = jarc.pwlArc.pts.param[1];
	    extrema[2] = jarc;
	}
	if ( jarc.tail()[1] >  toppt || 
	    (jarc.tail()[1] >= toppt && jarc.rhead()[1] >= toppt))  {
	    toppt = jarc.pwlArc.pts.param[1];
	    extrema[0] = jarc;
	}
    }
  }

  /**
   * Prints out the vertices of all pwl arcs on a loop.
   */
  public void    print( ) {
    Arc jarc = this;

    do {
      jarc.show( );
      jarc = jarc.next;
    } while (jarc != this);
  }

  public void    show( ) {
    System.out.println( "\tPWLARC NP: " + pwlArc.npts + " FL: 1");
    for( int i = 0; i < pwlArc.npts; i++ ) {
      System.out.println( "\t\tVERTEX " + pwlArc.pts[i].param[0] + " " +
                          pwlArc.pts[i].param[1] );
    }
  }

  /**
   * Attaches a pwl arc to an arc and mark it as a border arc.
   */
  public void    makeSide( PwlArc pwl, int arcSide ) {
    assert( pwl != 0);
    assert( pwlArc == 0 );
    assert( pwl.npts > 0 );
    assert( pwl.pts != 0);
    pwlArc = pwl;
    clearbezier();
    setside( arcSide );
  }

  public boolean      isTessellated()        { return (pwlArc != null); }
  public boolean      isbezier()             { return (type & bezier_tag) != 0; }
  public void         setbezier()            { type |= bezier_tag; }
  public void         clearbezier()          { type &= ~bezier_tag; }
  public long         npts()                 { return pwlArc.npts; }
  public TrimVertex[] pts()                  { return pwlArc.pts; }
  public float[]      tail()                 { return pwlArc.pts[0].param; }
  public float[]      head()                 { return next.pwlArc.pts[0].param; }
  public float[]      rhead()                { return pwlArc.pts[pwlArc.npts-1].param; }
  public long         ismarked()             { return type & arc_tag; }
  public void         setmark()              { type |= arc_tag; }
  public void         clearmark()            { type &= (~arc_tag); }
  public void         clearside()            { type &= ~(0x7 << 8); }
  public void         setside( int arcSide ) { clearside(); type |= (((long)arcSide)<<8); }
  public int          getside()              { return ((type>>8) & 0x7); }
  public int          getitail()             { return type & tail_tag; }
  public void         setitail()             { type |= tail_tag; }
  public void         clearitail()           { type &= (~tail_tag); }
}