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#include <math.h> //for fabs()
41f220fa62Smrg#include "glimports.h"
42f220fa62Smrg#include "zlassert.h"
43f220fa62Smrg#include "sampledLine.h"
44f220fa62Smrg
45f220fa62Smrgvoid sampledLine::setPoint(Int i, Real p[2])
46f220fa62Smrg{
47f220fa62Smrg  points[i][0]=p[0];
48f220fa62Smrg  points[i][1]=p[1];
49f220fa62Smrg}
50f220fa62Smrg
51f220fa62Smrg
52f220fa62Smrg/*insert this single line in front of the oldList*/
53f220fa62SmrgsampledLine* sampledLine::insert(sampledLine *oldList)
54f220fa62Smrg{
55f220fa62Smrg  next = oldList;
56f220fa62Smrg  return this;
57f220fa62Smrg}
58f220fa62Smrg
59f220fa62Smrgvoid sampledLine::deleteList()
60f220fa62Smrg{
61f220fa62Smrg  sampledLine *temp, *tempNext;
62f220fa62Smrg  for(temp = this; temp != NULL; temp = tempNext)
63f220fa62Smrg    {
64f220fa62Smrg      tempNext = temp->next;
65f220fa62Smrg      delete temp;
66f220fa62Smrg    }
67f220fa62Smrg}
68f220fa62Smrg
69f220fa62Smrg
70f220fa62Smrg/*space of points[][2] is allocated*/
71f220fa62SmrgsampledLine::sampledLine(Int n_points)
72f220fa62Smrg{
73f220fa62Smrg  npoints = n_points;
74f220fa62Smrg  points = (Real2*) malloc(sizeof(Real2) * n_points);
75f220fa62Smrg  assert(points);
76f220fa62Smrg  next = NULL;
77f220fa62Smrg}
78f220fa62Smrg
79f220fa62Smrg/*space of points[][2] is allocated and
80f220fa62Smrg *points are copied
81f220fa62Smrg */
82f220fa62SmrgsampledLine::sampledLine(Int n_points, Real2 pts[])
83f220fa62Smrg{
84f220fa62Smrg  int i;
85f220fa62Smrg  npoints = n_points;
86f220fa62Smrg  points = (Real2*) malloc(sizeof(Real2) * n_points);
87f220fa62Smrg  assert(points);
88f220fa62Smrg  for(i=0; i<npoints; i++) {
89f220fa62Smrg    points[i][0] = pts[i][0];
90f220fa62Smrg    points[i][1] = pts[i][1];
91f220fa62Smrg  }
92f220fa62Smrg  next = NULL;
93f220fa62Smrg}
94f220fa62Smrg
95f220fa62SmrgsampledLine::sampledLine(Real pt1[2], Real pt2[2])
96f220fa62Smrg{
97f220fa62Smrg  npoints = 2;
98f220fa62Smrg  points = (Real2*) malloc(sizeof(Real2) * 2);
99f220fa62Smrg  assert(points);
100f220fa62Smrg  points[0][0] = pt1[0];
101f220fa62Smrg  points[0][1] = pt1[1];
102f220fa62Smrg  points[1][0] = pt2[0];
103f220fa62Smrg  points[1][1] = pt2[1];
104f220fa62Smrg  next = NULL;
105f220fa62Smrg}
106f220fa62Smrg
107f220fa62Smrg//needs tp call init to setup
108f220fa62SmrgsampledLine::sampledLine()
109f220fa62Smrg{
110f220fa62Smrg  npoints = 0;
111f220fa62Smrg  points = NULL;
112f220fa62Smrg  next = NULL;
113f220fa62Smrg}
114f220fa62Smrg
115f220fa62Smrg//warning: ONLY pointer is copies!!!
116f220fa62Smrgvoid sampledLine::init(Int n_points, Real2 *pts)
117f220fa62Smrg{
118f220fa62Smrg  npoints = n_points;
119f220fa62Smrg  points = pts;
120f220fa62Smrg}
121f220fa62Smrg
122f220fa62Smrg/*points[] is dealocated
123f220fa62Smrg */
124f220fa62SmrgsampledLine::~sampledLine()
125f220fa62Smrg{
126f220fa62Smrg  free(points);
127f220fa62Smrg}
128f220fa62Smrg
129f220fa62Smrgvoid sampledLine::print()
130f220fa62Smrg{
131f220fa62Smrg  int i;
132f220fa62Smrg  printf("npoints=%i\n", npoints);
133f220fa62Smrg
134f220fa62Smrg  for(i=0; i<npoints; i++){
135f220fa62Smrg    printf("(%f,%f)\n", points[i][0], points[i][1]);
136f220fa62Smrg  }
137f220fa62Smrg
138f220fa62Smrg}
139f220fa62Smrg
140f220fa62Smrgvoid sampledLine::tessellate(Real u_reso, Real v_reso)
141f220fa62Smrg{
142f220fa62Smrg  int i;
143f220fa62Smrg
144f220fa62Smrg  Int nu, nv, n;
145f220fa62Smrg  nu = 1+(Int) (fabs((points[npoints-1][0] - points[0][0])) * u_reso);
146f220fa62Smrg  nv = 1+(Int) (fabs((points[npoints-1][1] - points[0][1])) * v_reso);
147f220fa62Smrg
148f220fa62Smrg  if(nu > nv) n = nu;
149f220fa62Smrg  else
150f220fa62Smrg    n = nv;
151f220fa62Smrg  if(n<1)
152f220fa62Smrg    n = 1;
153f220fa62Smrg  //du dv could be negative
154f220fa62Smrg  Real du = (points[npoints-1][0] - points[0][0])/n;
155f220fa62Smrg  Real dv = (points[npoints-1][1] - points[0][1])/n;
156f220fa62Smrg  Real2 *temp = (Real2*) malloc(sizeof(Real2) * (n+1));
157f220fa62Smrg  assert(temp);
158f220fa62Smrg
159f220fa62Smrg  Real u,v;
160f220fa62Smrg  for(i=0, u=points[0][0], v=points[0][1]; i<n; i++, u+=du, v+=dv)
161f220fa62Smrg    {
162f220fa62Smrg      temp[i][0] = u;
163f220fa62Smrg      temp[i][1] = v;
164f220fa62Smrg    }
165f220fa62Smrg  temp[n][0] = points[npoints-1][0];
166f220fa62Smrg  temp[n][1] = points[npoints-1][1];
167f220fa62Smrg
168f220fa62Smrg  free(points);
169f220fa62Smrg
170f220fa62Smrg  npoints = n+1;
171f220fa62Smrg  points = temp;
172f220fa62Smrg
173f220fa62Smrg}
174f220fa62Smrg
175f220fa62Smrgvoid sampledLine::tessellateAll(Real u_reso, Real v_reso)
176f220fa62Smrg{
177f220fa62Smrg  sampledLine* temp;
178f220fa62Smrg  for(temp = this; temp != NULL; temp = temp->next)
179f220fa62Smrg    {
180f220fa62Smrg      temp->tessellate(u_reso, v_reso);
181f220fa62Smrg    }
182f220fa62Smrg}
183