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 * knotvector.c++
37f220fa62Smrg *
38f220fa62Smrg */
39f220fa62Smrg
40f220fa62Smrg#include "glimports.h"
41f220fa62Smrg#include "mystdio.h"
42f220fa62Smrg#include "myassert.h"
43f220fa62Smrg#include "knotvector.h"
44f220fa62Smrg#include "defines.h"
45f220fa62Smrg
46f220fa62Smrg#ifdef __WATCOMC__
47f220fa62Smrg#pragma warning 726 10
48f220fa62Smrg#endif
49f220fa62Smrg
50f220fa62Smrgvoid Knotvector::init( long _knotcount, long _stride, long _order, INREAL *_knotlist )
51f220fa62Smrg{
52f220fa62Smrg    knotcount = _knotcount;
53f220fa62Smrg    stride = _stride;
54f220fa62Smrg    order = _order;
55f220fa62Smrg    knotlist = new Knot[_knotcount];
56f220fa62Smrg    assert( knotlist != 0 );
57f220fa62Smrg
58f220fa62Smrg    for( int i = 0; i != _knotcount; i++ )
59f220fa62Smrg	knotlist[i] = (Knot) _knotlist[i];
60f220fa62Smrg}
61f220fa62Smrg
62f220fa62SmrgKnotvector::Knotvector( void )
63f220fa62Smrg{
64f220fa62Smrg    knotcount = 0;
65f220fa62Smrg    stride = 0;
66f220fa62Smrg    order = 0;
67f220fa62Smrg    knotlist = 0;
68f220fa62Smrg}
69f220fa62Smrg
70f220fa62SmrgKnotvector::~Knotvector( void )
71f220fa62Smrg{
72f220fa62Smrg    if( knotlist ) delete[] knotlist;
73f220fa62Smrg}
74f220fa62Smrg
75f220fa62Smrgint Knotvector::validate( void )
76f220fa62Smrg{
77f220fa62Smrg   /* kindex is used as an array index so subtract one first,
78f220fa62Smrg     * this propagates throughout the code so study carefully */
79f220fa62Smrg    long	kindex = knotcount-1;
80f220fa62Smrg
81f220fa62Smrg    if( order < 1 || order > MAXORDER ) {
82f220fa62Smrg	// spline order un-supported
83f220fa62Smrg	return( 1 );
84f220fa62Smrg    }
85f220fa62Smrg
86f220fa62Smrg    if( knotcount < (2 * order) ) {
87f220fa62Smrg	// too few knots
88f220fa62Smrg	return( 2 );
89f220fa62Smrg    }
90f220fa62Smrg
91f220fa62Smrg    if( identical( knotlist[kindex-(order-1)], knotlist[order-1]) ) {
92f220fa62Smrg	// valid knot range is empty
93f220fa62Smrg	return( 3 );
94f220fa62Smrg    }
95f220fa62Smrg
96f220fa62Smrg    for( long i = 0; i < kindex; i++)
97f220fa62Smrg	if( knotlist[i] > knotlist[i+1] ) {
98f220fa62Smrg	    // decreasing knot sequence
99f220fa62Smrg	    return( 4 );
100f220fa62Smrg	}
101f220fa62Smrg
102f220fa62Smrg    /* check for valid multiplicity */
103f220fa62Smrg
104f220fa62Smrg    /*	kindex is currently the index of the last knot.
105f220fa62Smrg     *	In the next loop  it is decremented to ignore the last knot
106f220fa62Smrg     *	and the loop stops when kindex	is 2 so as to ignore the first
107f220fa62Smrg     *	knot as well.  These knots are not used in computing
108f220fa62Smrg     *	knot multiplicities.
109f220fa62Smrg     */
110f220fa62Smrg
111f220fa62Smrg    long multi = 1;
112f220fa62Smrg    for( ; kindex >= 1; kindex-- ) {
113f220fa62Smrg	if( knotlist[kindex] - knotlist[kindex-1] < TOLERANCE ) {
114f220fa62Smrg	    multi++;
115f220fa62Smrg	    continue;
116f220fa62Smrg	}
117f220fa62Smrg	if ( multi > order ) {
118f220fa62Smrg	    // knot multiplicity greater than order of spline
119f220fa62Smrg	    return( 5 );
120f220fa62Smrg	}
121f220fa62Smrg	multi = 1;
122f220fa62Smrg    }
123f220fa62Smrg
124f220fa62Smrg    if ( multi > order ) {
125f220fa62Smrg	// knot multiplicity greater than order of spline
126f220fa62Smrg	return( 5 );
127f220fa62Smrg    }
128f220fa62Smrg
129f220fa62Smrg    return 0;
130f220fa62Smrg}
131f220fa62Smrg
132f220fa62Smrgvoid Knotvector::show( const char *msg )
133f220fa62Smrg{
134f220fa62Smrg#ifndef NDEBUG
135f220fa62Smrg    _glu_dprintf( "%s\n", msg );
136f220fa62Smrg    _glu_dprintf( "order = %ld, count = %ld\n", order, knotcount );
137f220fa62Smrg
138f220fa62Smrg    for( int i=0; i<knotcount; i++ )
139f220fa62Smrg	_glu_dprintf( "knot[%d] = %g\n", i, knotlist[i] );
140f220fa62Smrg#endif
141f220fa62Smrg}
142f220fa62Smrg
143