1f220fa62Smrg/*
2f220fa62Smrg** License Applicability. Except to the extent portions of this file are
3f220fa62Smrg** made subject to an alternative license as permitted in the SGI Free
4f220fa62Smrg** Software License B, Version 1.1 (the "License"), the contents of this
5f220fa62Smrg** file are subject only to the provisions of the License. You may not use
6f220fa62Smrg** this file except in compliance with the License. You may obtain a copy
7f220fa62Smrg** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8f220fa62Smrg** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9f220fa62Smrg**
10f220fa62Smrg** http://oss.sgi.com/projects/FreeB
11f220fa62Smrg**
12f220fa62Smrg** Note that, as provided in the License, the Software is distributed on an
13f220fa62Smrg** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14f220fa62Smrg** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15f220fa62Smrg** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16f220fa62Smrg** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17f220fa62Smrg**
18f220fa62Smrg** Original Code. The Original Code is: OpenGL Sample Implementation,
19f220fa62Smrg** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20f220fa62Smrg** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21f220fa62Smrg** Copyright in any portions created by third parties is as indicated
22f220fa62Smrg** elsewhere herein. All Rights Reserved.
23f220fa62Smrg**
24f220fa62Smrg** Additional Notice Provisions: The application programming interfaces
25f220fa62Smrg** established by SGI in conjunction with the Original Code are The
26f220fa62Smrg** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27f220fa62Smrg** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28f220fa62Smrg** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29f220fa62Smrg** Window System(R) (Version 1.3), released October 19, 1998. This software
30f220fa62Smrg** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31f220fa62Smrg** published by SGI, but has not been independently verified as being
32f220fa62Smrg** compliant with the OpenGL(R) version 1.2.1 Specification.
33f220fa62Smrg**
34f220fa62Smrg*/
35f220fa62Smrg/*
36f220fa62Smrg*/
37f220fa62Smrg
38f220fa62Smrg#include <stdlib.h>
39f220fa62Smrg#include <stdio.h>
40f220fa62Smrg
41f220fa62Smrg#include "partitionX.h"
42f220fa62Smrg
43f220fa62Smrg#define CONCAVITY_ZERO 1.0e-6 //this number is used to test whether a vertex is concave (refelx)
44f220fa62Smrg                              //or not. The test needs to compute the area of the three adjacent
45f220fa62Smrg                              //vertices to see if the are is positive or negative.
46f220fa62Smrg
47f220fa62SmrgInt isCuspX(directedLine *v)
48f220fa62Smrg{
49f220fa62Smrg  //if v->prev <= v && v->next <= v
50f220fa62Smrg  //|| v->prev >= v && v->next >= v
51f220fa62Smrg  Real* T = v->head();
52f220fa62Smrg  Real* P = v->getPrev()->head();
53f220fa62Smrg  Real* N = v->getNext()->head();
54f220fa62Smrg  if(
55f220fa62Smrg     (compV2InX(T,P) != -1 &&
56f220fa62Smrg      compV2InX(T,N) != -1
57f220fa62Smrg      ) ||
58f220fa62Smrg     (compV2InX(T,P) != 1 &&
59f220fa62Smrg      compV2InX(T,N) != 1
60f220fa62Smrg      )
61f220fa62Smrg     )
62f220fa62Smrg    return 1;
63f220fa62Smrg  else
64f220fa62Smrg    return 0;
65f220fa62Smrg}
66f220fa62Smrg
67f220fa62SmrgInt isReflexX(directedLine* v)
68f220fa62Smrg{
69f220fa62Smrg  Real* A = v->getPrev()->head();
70f220fa62Smrg  Real* B = v->head();
71f220fa62Smrg  Real* C = v->tail();
72f220fa62Smrg  Real Bx,By, Cx, Cy;
73f220fa62Smrg  //scale them in case they are too small
74f220fa62Smrg  Bx = 10*(B[0] - A[0]);
75f220fa62Smrg  By = 10*(B[1] - A[1]);
76f220fa62Smrg  Cx = 10*(C[0] - A[0]);
77f220fa62Smrg  Cy = 10*(C[1] - A[1]);
78f220fa62Smrg
79f220fa62Smrg  if(Bx*Cy - Cx*By < -CONCAVITY_ZERO) return 1;
80f220fa62Smrg  else return 0;
81f220fa62Smrg}
82f220fa62Smrg
83f220fa62Smrg
84f220fa62Smrg/*return
85f220fa62Smrg *0: not-cusp
86f220fa62Smrg *1: interior cusp
87f220fa62Smrg *2: exterior cusp
88f220fa62Smrg */
89f220fa62SmrgInt cuspTypeX(directedLine *v)
90f220fa62Smrg{
91f220fa62Smrg  if(! isCuspX(v)) return 0;
92f220fa62Smrg  else
93f220fa62Smrg    {
94f220fa62Smrg//printf("isCusp,%f,%f\n", v->head()[0], v->head()[1]);
95f220fa62Smrg      if(isReflexX(v))
96f220fa62Smrg	{
97f220fa62Smrg//	  printf("isReflex\n");
98f220fa62Smrg	  return 1;
99f220fa62Smrg	}
100f220fa62Smrg      else
101f220fa62Smrg	{
102f220fa62Smrg//	  printf("not isReflex\n");
103f220fa62Smrg	  return 2;
104f220fa62Smrg	}
105f220fa62Smrg    }
106f220fa62Smrg}
107f220fa62Smrg
108f220fa62SmrgInt numInteriorCuspsX(directedLine *polygon)
109f220fa62Smrg{
110f220fa62Smrg  directedLine *temp;
111f220fa62Smrg  int ret = 0;
112f220fa62Smrg  if(cuspTypeX(polygon) == 1)
113f220fa62Smrg    ret++;
114f220fa62Smrg  for(temp = polygon->getNext(); temp != polygon; temp = temp->getNext())
115f220fa62Smrg    if(cuspTypeX(temp) == 1)
116f220fa62Smrg      ret++;
117f220fa62Smrg  return ret;
118f220fa62Smrg}
119f220fa62Smrg
120f220fa62Smrg
121f220fa62Smrgvoid findInteriorCuspsX(directedLine *polygon, Int& ret_n_interior_cusps,
122f220fa62Smrg			directedLine** ret_interior_cusps)
123f220fa62Smrg{
124f220fa62Smrg  directedLine *temp;
125f220fa62Smrg  ret_n_interior_cusps = 0;
126f220fa62Smrg  if(cuspTypeX(polygon) == 1)
127f220fa62Smrg    {
128f220fa62Smrg      ret_interior_cusps[ret_n_interior_cusps++] = polygon;
129f220fa62Smrg    }
130f220fa62Smrg  for(temp = polygon->getNext(); temp != polygon; temp = temp->getNext())
131f220fa62Smrg    if(cuspTypeX(temp) == 1)
132f220fa62Smrg      {
133f220fa62Smrg	ret_interior_cusps[ret_n_interior_cusps++] = temp;
134f220fa62Smrg      }
135f220fa62Smrg}
136f220fa62Smrg
137f220fa62SmrgdirectedLine* findDiagonal_singleCuspX(directedLine* cusp)
138f220fa62Smrg{
139f220fa62Smrg  directedLine* temp;
140f220fa62Smrg  Int is_minimal = ((compV2InX(cusp->head(), cusp->tail()) == -1)? 1:0);
141f220fa62Smrg
142f220fa62Smrg  if(is_minimal)
143f220fa62Smrg    for(temp = cusp->getNext(); temp != cusp; temp = temp->getNext())
144f220fa62Smrg      {
145f220fa62Smrg	if(compV2InX(cusp->head(), temp->head()) == 1)
146f220fa62Smrg	  {
147f220fa62Smrg	    return temp;
148f220fa62Smrg	  }
149f220fa62Smrg      }
150f220fa62Smrg  else //is maxmal
151f220fa62Smrg    for(temp = cusp->getNext(); temp != cusp; temp = temp->getNext())
152f220fa62Smrg      {
153f220fa62Smrg	if(compV2InX(cusp->head(), temp->head()) == -1)
154f220fa62Smrg	  {
155f220fa62Smrg	    return temp;
156f220fa62Smrg	  }
157f220fa62Smrg      }
158f220fa62Smrg  return NULL;
159f220fa62Smrg}
160f220fa62Smrg
161f220fa62Smrg
162f220fa62Smrg
163