1\chapter{Tutorial programs} 2 3This release of SISL is bundled with a number of sample programs which are intended 4to make the user more familiar with the use of the API, as well as demonstrating 5some of its capabilities. 6 7\section{Compiling the programs} 8Makefiles are provided to compile all provided source code, included the sample programs. 9Refer to (\ref{compile}) for details on using the provided makefiles. Please note 10that since these executables link both with SISL 4.4 and the `streaming' library, these 11must be compiled first.\\ 12 13The compilation of the example code has been verified for the GCC 3.3.3 compiler 14and the Microsoft Visual C++ .NET 2002 compiler, but it should be general enough to 15compile relatively troublefree on most platforms. 16 17\section{Description and commentaries on the sample programs} 18 19The example programs are named \verb/example01/ through \verb/example15/. Each of the 20program demonstrates the use of a single or a couple of SISL functions. The programs 21produces output files that contain geometric objects in the \verb/Go/-format, which 22can then be visualised by the provided KRULL-viewer. 23 24To keep things as simple as possible, the example programs (with the exception of 25\verb/example15/) take no command line arguments. Instead, upon execution they inform 26the user about what they are about to do, and which files will be read from and written 27to. The names of the input and output files are hard-coded in each example, but the 28user can experiment by changing the name of these files if she wants to. Several of 29the sample programs rely upon files generated by earlier examples, so the user should 30make sure she runs through them in chronological order. 31 32\subsection{example01.C} 33 34\subsubsection{What it does} 35This program demonstrates how to directly specify a spline curve by providing the 36position of control points and a knotvector (parametrization). It generates such 37a curve by using hard-coded values as input to the SISL \verb/newCurve/ routine. 38\subsubsection{What it demonstrates} 39\begin{enumerate} 40\item How control points and knotvectors are specified in memory 41\item How to use the \verb/newCurve/ routine 42\item How to clean up memory using \verb/freeCurve/ 43\end{enumerate} 44 45\subsubsection{Input/output} 46The program takes no input files.\\ 47The program generates the files \verb/example1_curve.g2/ and \verb/example1_points.g2/. 48The former contains the curve object and the latter contains the control points, expressed 49in the \verb/Go/-format. 50 51\subsection{example02.C} 52 53\subsubsection{What it does} 54This program demonstrates one of the simplest \emph{interpolation} cases for spline 55curves in SISL. A sequence of 6 3D-points are provided (hardcoded), and the routine 56generates a spline curve that fits exactly through these points. Note that this is a 57simple example of a more general routine, which can also take into consideration 58tangents, end point conditions, etc. 59 60\subsubsection{What it demonstrates} 61\begin{enumerate} 62\item The use of the SISL routine \verb/s1356/ for interpolating points with a curve 63\end{enumerate} 64\subsubsection{Input/output} 65The program takes no input files.\\ 66The program generates the file \verb/example2_points.g2/ and \verb/example2_curve.g2/. 67The first file contains the points to be interpolated, and the second file contains the 68generated curve. 69 70\subsection{example03.C} 71 72\subsubsection{What it does} 73This program creates a so-called \emph{blend-curve} between two other curves, creating a 74smooth connection between these. In this program, the blend curve connects the \emph{end 75points} of the two other curves, but in its generality, the routine can be used to create 76blend curves connecting to any point on the other curves. 77 78\subsubsection{What it demonstrates} 79\begin{enumerate} 80\item What a blend curve is and how it can be specified 81\item The use of the SISL routine \verb/s1606/ which computes the blend curve 82\item The use of the SISL routine \verb/s1227/ which evaluates points (and derivatives) 83on a spline curve 84\item How to directly access data members of the \verb/SISLCurve/ struct. 85\end{enumerate} 86\subsubsection{Input/output} 87The program takes as input the files \verb/example1_curve.g2/ and \verb/example2_points.g2/, 88which are respectively generated by the programs \verb/example01/ and \verb/example02/. 89The generated blend curve will be saved to the file \verb/example3_curve.g2/. 90 91\subsection{example04.C} 92 93\subsubsection{What it does} 94This program generates an \emph{offset curve} from another curve. An offset curve is 95specified as having a fixed distance in a specified direction from the original curve. 96The generated offset curve will not be exact, as this would in general be impossible using 97spline-function. We can however obtain an approximation within a user-specified tolerance. 98 99\subsubsection{What it demonstrates} 100\begin{enumerate} 101\item What an offset curve is and how it can be specified 102\item The way in which many SISL routines deal with geometric tolerances 103\item The use of the SISL routine \verb/s1360/ which computes the offset curve within a 104specified, geometric tolerance 105\end{enumerate} 106\subsubsection{Input/output} 107The original curve is read from the file \verb/example1_curve.g2/, which is generated by 108the program \verb/example01/. The resulting approximation of the offset curve will be 109written to the file \verb/example4_curve.g2/. 110 111\subsection{example05.C} 112 113\subsubsection{What it does} 114This program generates a family of conic section curves, which are represented as rational 115splines. Conic sections can be \emph{exactly} represented with such splines, so no geometric 116tolerance specification is needed. The program will generate three ellipse segments, one 117parabola segment and three hyperbola segments, based on internal, hard-coded data. 118 119\subsubsection{What it demonstrates} 120\begin{enumerate} 121\item The use of the SISL routine \verb/s1011/ to generate all kinds of conic sections 122\item The important fact that conic sections can be exactly represented by rational splines 123\item How a single \emph{shape} parameter can specify whether the generated curve will be an 124ellipse, a parabola or a hyperbola 125\end{enumerate} 126\subsubsection{Input/output} 127The program takes no input files.\\ 128The program generates the file \verb/example5_curve.g2/ which contains all the generated curves. 129 130\subsection{example06.C} 131 132\subsubsection{What it does} 133This program generates two curves (from internal, hardcoded data), and computes their 134intersections. Computation of intersections is an extremely important part of SISL, although 135the intersection of two curves is a minor problem in this respect. 136\subsubsection{What it demonstrates} 137\begin{enumerate} 138\item The use of the SISL routine \verb/s1857/ for computing the intersection points between 139two given spline curves 140\item Underlines the fact that the detected intersection points are returned as parameter values, 141and have to be evaluated in order to find their 3D positions 142\item How to clean up an array of intersection curves (SISLIntcurve), although, in 143this example, this array will already be empty 144\end{enumerate} 145\subsubsection{Input/output} 146The program takes no input files (the data for the curves is hard-coded). \\ 147The generated curves will be written to the files \verb/example6_curve_1.g2/ and \\ 148\verb/example6_curve_2.g2/. The intersection point positions will be written to the file 149\verb/example6_isectpoints.g2/. 150 151\subsection{example07.C} 152 153\subsubsection{What it does} 154This is a very short and simple program that calculates the arc length of a curve. 155\subsubsection{What it demonstrates} 156\begin{enumerate} 157\item The use of the SISL routine \verb/s1240/ for computing the length of a spline curve 158\end{enumerate} 159\subsubsection{Input/output} 160The curve whose length is calculated is read from the file \verb/example6_curve_1/, which 161has been generated by the sample program \verb/example06/. The calculated length will be 162written to standard output. 163 164\subsection{example08.C} 165 166\subsubsection{What it does} 167This program generates two non-intersecting spline curves (from internal, hard-coded cata), and 168computes their mutual closest point. The call is very similar to the one in \verb/example06/, 169where we wanted to compute curve intersections. 170\subsubsection{What it demonstrates} 171\begin{enumerate} 172\item The use of the SISL routine \verb/s1955/ for locating the closest points of two curves. 173\end{enumerate} 174\subsubsection{Input/output} 175As the curves are specified directly by internal data, no input files are needed.\\ 176The two generated curves will be saved to the two files \verb/example8_curve_1.g2/ and 177\verb/example8_curve_2.g2/. The closest points will be written to the file \\ 178\verb/example8_closestpoints.g2/. 179 180\subsection{example09.C} 181 182\subsubsection{What it does} 183This program generates four different surfaces interpolating an array of spatial points. 184The surfaces have different spline order, so that even though they interpolate the same 185points, they have different shapes. 186\subsubsection{What it demonstrates} 187\begin{enumerate} 188\item The use of the SISL routine \verb/s1537/ for generating an interpolating surface to a grid 189of points 190\item The effect of the spline order on the interpolating surface 191\end{enumerate} 192\subsubsection{Input/output} 193The program takes no input files (the points to be interpolated are hard-coded). \\ 194The program creates two data files: \verb/example9_points.g2/, which contains all the 195interpolated points, and \verb/example9_surf.g2/, which contains the four generated 196surfaces. 197 198\subsection{example10.C} 199 200\subsubsection{What it does} 201This program generates a sequence of spline curves. Moreover, it generates a \emph{lofted 202surface} interpolating these curves. The lofted surface has the original sequence of curves 203as isoparametric curves in one of its parameters. 204\subsubsection{What it demonstrates} 205\begin{enumerate} 206\item The use of the SISL routine \verb/s1538/ for generating lofted spline surfaces 207\item Gives a good example of what a lofted surface looks like 208\end{enumerate} 209\subsubsection{Input/output} 210The program takes no input files (the curves to be interpolated are hard-coded).\\ 211The program creates two data files: \verb/example10_curves.g2/, containing the generated 212sequence of curves, and \verb/example10_surf.g2/, containing the lofted surface. 213 214\subsection{example11.C} 215 216\subsubsection{What it does} 217This program generates a cylindrical surface with an oval base. 218\subsubsection{What it demonstrates} 219\begin{enumerate} 220\item The use of the SISL routine \verb/s1021/ for generating cylindrical surfaces 221\item The fact that cylindrical surfaces are exactly representable as rational spline surfaces 222\end{enumerate} 223\subsubsection{Input/output} 224The program takes no input files.\\ 225The program creates one data file: \verb/example11_surf.g2/, containing the generated surface. 226 227\subsection{example12.C} 228 229\subsubsection{What it does} 230This program finds the intersection points between a curve and a surface. The curve and the 231surface in question have been defined by previous example programs. 232\subsubsection{What it demonstrates} 233\begin{enumerate} 234\item The use of the SISL routine \verb/s1858/ for computing intersection points between a 235curve and a surface 236\end{enumerate} 237\subsubsection{Input/output} 238The curve and the surface in question are read from the files \verb/example4_curve.g2/ 239and \verb/example10_surf.g2/, respectively generated by the sample programs 240\verb/example04/ and \verb/example10/. The found intersections are written to 241the file \\ 242\verb/example12_isectpoints.g2/. 243 244\subsection{example13.C} 245 246\subsubsection{What it does} 247This program computes all intersection curves between two surfaces. This is a nontrivial 248task in geometrical modeling. The problem is twofold. The first problem is to determine 249the number of intersections, and their topology. The region of an intersection can be either 250a point, a curve and a surface. In the two latter cases, the shape of the region can usually 251only be approximated. We do not know a priori how many separate intersections there exists 252between two surfaces, so we have to look systematically for them. Intersection curves can 253take the form either as closed loops on the interior of the surfaces, or as curves running 254from the surface edges. When we have successfully determined the topology of the intersections, 255the second problem is to determine their acutal shape. This is usually done by \emph{marching 256techniques}. However, we may run into problems with 'degenerated' surfaces, or surfaces being 257close to coplanar in the intersection. 258\subsubsection{What it demonstrates} 259\begin{enumerate} 260\item The use of the SISL routine \verb/s1859/ for determining the topology of the intersections 261between two spline surfaces 262\item The use of the SISL routine \verb/s1310/ for marching out the detected curves after their 263topologies have been determined 264\end{enumerate} 265\subsubsection{Input/output} 266The two surfaces have been generated by the previous sample programs \verb/example10/ and 267\verb/example11/, and can be found in the files \verb/example10_surf.g2/ and \\ 268\verb/example11_surf.g2/. The resulting intersection curves will be written to the file 269\verb/example13_isectcurves.g2/. 270 271\subsection{example14.C} 272 273\subsubsection{What it does} 274This program demonstrates one of the data reduction techniques of SISL. As input data, it first 275generates a dense point set by sampling from a (predefined) spline curve. Then, using this 276data, it attempts to generate a new spline curve that fits closely to these samples, while using 277as few control points as possible. Since we know that in this case the data points come from 278a simple spline curve, it should be no surprise that the generated curve will have approximately 279the same expression as the sampled curve (and thus reduce the quantity of data substantially 280compared to what is needed to store the points). However, data reduction can be obtained on any 281sufficiently smooth point set, even if it originates from other processes. 282 283\subsubsection{What it demonstrates} 284\begin{enumerate} 285\item The use of the SISL routine \verb/s1961/ for generating approximating spline curves through 286a set of data, using as few control points as possible. 287\item The power of this data reduction technique on smooth point data. 288\end{enumerate} 289\subsubsection{Input/output} 290The program takes no input files, as the curve to be sampled from is hard-coded. \\ 291The sampled points will be written to the file \verb/example14_points.g2/, and the obtained 292curve will be stored in \verb/example14_curve.g2/. 293 294\subsection{example15.C} 295 296\subsubsection{What it does} 297This is the last of the sample programs, and by far the most complicated. It aims not only to 298demonstrate a certain feature of SISL, but to show how this feature can be used for a purpose 299(raytracing). Moreover, it demonstrates two ways of achieveing this, one slow and robust method 300and one rapid but fragile method. \\ 301Raytracing can be seen as the process of determining what an object 'looks like' from a 302certain viewpoint, through a certain 'window', as illustrated below. 303\begin{figure}[h] 304\begin{center} 305\includegraphics[width=2.3in, height=2in,angle = 0]{raytracing.ps} 306\end{center} 307\end{figure} 308Lines ('rays') are extended from the viewpoint through a dense grid of points on the window, 309and checked for intersection with the object. If such an intersection exists, it should be 310registered as a point on the object 'visible' from the viewpoint. In computer graphics, these 311points are projected back on the window, which becomes a 2D image that can be displayed on the 312computer screen. For our purposes, we refrain from doing this projection, and store the 313full 3D coordinates of the detected point.\\ 314Note that a ray may intersect the object more than once. In these cases, the intersection point 315closest to the viewpoint is chosen, as the other points are 'hidden' by it. As mentioned above, 316there are two raytracing routines in this example program. The robust routine calculates all 317possible intersection points for each ray, and then choses the nearest one. This should always 318work, but can be slow since no information is re-used. When we have found an intersection point 319for a given ray, we can usually expect that the next, neighouring ray will intersect in a point 320close to the one already found. If this is the case, it would be speedier to use a local 321algorithm that converges on the intersection point quickly given a good initial guess. This is 322the basis for our 'quick' routine. This routine uses the robust raytracing algorithm to find 323the first point on a surface, and then it switches over to the fast method as long as it is 324possible to do so. However, since the quick method never finds more than one intersection point, 325and since a ray may generally intersect an object more than once, we have no guarantee that 326the point found is the one truly visible from the viewpoint. There are some checking procedures 327that make things better, but we still have no guarantee. If the user inspects the results 328obtained, he will notice this problem even on the simple example given here. In general, it 329can be said that the rapid algorithm should only be used in some special cases, where we know 330for a fact that any ray from the viewpoint will not intersect the surface more than once.\\ 331 332This is the only of the example programs that can be run with a command line argument. If the 333first argument is \verb/q/, then the quick raytracing routine will be invoked. Else, the 334robust and slow routine is used. 335 336\subsubsection{What it demonstrates} 337\begin{enumerate} 338\item The basic setting and principe of a raytracer, with a defined viewpoint, window and 339intersection with rays. 340\item The use of the SISL routine \verb/s1856/, which calculates all intersections between a 341spline surface and a line. 342\item The use of the SISL routine \verb/s1518/, which converges to an intersection between 343a spline surface and a line, given a good initial guess. 344\end{enumerate} 345\subsubsection{Input/output} 346The surface to be raytraced is read from the file \verb/example10_surf.g2/, generated by 347the \verb/example10/ program. The other parameters necessary for the raytracing are hard- 348coded (viewpoint, view window, resolution, etc.). The resulting points are written to the 349file \verb/example15_points.g2/.