2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 1998-2001 - ENPC - Jean-Philippe Chancelier
4 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 /*------------------------------------------------------------------------
16 --------------------------------------------------------------------------*/
18 #include "math_graphics.h"
21 * we use spConfig.h for machine constants
22 * XXX : spConfig should be merged and unified
23 * with other machine constant scilab code
26 #define spINSIDE_SPARSE
27 #include "../../sparse/includes/spConfig.h"
29 double Mini(double *vect, integer n)
34 for (i = 0 ; i < n ; i++)
35 /* if ( isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] < vmin) */
36 if ( finite(vect[i])== 1 && vect[i] < vmin)
41 double Maxi(double *vect,integer n)
46 for (i =0 ; i < n ; i++)
47 /* if ( isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] > maxi) */
48 if ( finite(vect[i])== 1 && vect[i] > maxi)
53 /*----------------------------------------------------------------------------*/
55 /* perform the rotation of point from to point dest */
56 void rotate2D( double from[2], double center[2], double angle, double dest[2] )
58 rotate2Dim( from, center, cos( angle ), sin( angle ), dest ) ;
61 /*----------------------------------------------------------------------------*/
62 /* perform the rotation of point from to point to. */
63 /* the angle is directly given with its sine and cosine for speed */
64 void rotate2Dim( double from[2] ,
72 /* put the center to (0,0) */
73 diff[0] = from[0] - center[0] ;
74 diff[1] = from[1] - center[1] ;
76 /* turn and translate back */
77 dest[0] = diff[0] * cosAngle - diff[1] * sinAngle + center[0] ;
78 dest[1] = diff[0] * sinAngle + diff[1] * cosAngle + center[1] ;
80 /*----------------------------------------------------------------------------*/
81 /* perform the rotation of point from to point dest given in int with angle in radian */
82 void iRotate2D( int from[2], int center[2], double angle, int dest[2] )
84 iRotate2Dim( from, center, cos( angle ), sin( angle ), dest ) ;
87 /*----------------------------------------------------------------------------*/
88 /* perform the rotation of point from to point to. */
89 /* the angle is directly given with its sine and cosine for speed */
90 void iRotate2Dim( int from[2] ,
98 /* put the center to (0,0) */
99 diff[0] = from[0] - center[0] ;
100 diff[1] = from[1] - center[1] ;
102 /* turn and translate back */
103 dest[0] = round( diff[0] * cosAngle - diff[1] * sinAngle + center[0] ) ;
104 dest[1] = round( diff[0] * sinAngle + diff[1] * cosAngle + center[1] ) ;
106 /*----------------------------------------------------------------------------*/
107 /* perform an homethety point from to point dest. The 2 factors stand for the ration */
108 /* along the 2 coordinates */
109 void homothety2D( double from[2], double center[2], double factors[2], double dest[2] )
111 dest[0] = center[0] + factors[0] * ( from[0] - center[0] ) ;
112 dest[1] = center[1] + factors[1] * ( from[1] - center[1] ) ;
114 /*----------------------------------------------------------------------------*/
115 /* perform an homethety point from to point dest given in pixels. */
116 /* The 2 factors stand for the ration along the 2 coordinates */
117 void iHomothety2D( int from[2], int center[2], double factors[2], int dest[2] )
119 dest[0] = round( center[0] + factors[0] * ( from[0] - center[0] ) ) ;
120 dest[1] = round( center[1] + factors[1] * ( from[1] - center[1] ) ) ;
122 /*----------------------------------------------------------------------------*/
123 /* perform the translation of point from to point to with vector trans */
124 void translate2D( double from[2], double trans[2], double dest[2] )
126 dest[0] = from[0] + trans[0] ;
127 dest[1] = from[1] + trans[1] ;
129 /*----------------------------------------------------------------------------*/
130 void iTranslate2D( int from[2], int trans[2], int dest[2] )
132 dest[0] = from[0] + trans[0] ;
133 dest[1] = from[1] + trans[1] ;
135 /*----------------------------------------------------------------------------*/
136 void vectSubstract2D(const double vect1[2], const double vect2[], double res[2])
138 res[0] = vect1[0] - vect2[0];
139 res[1] = vect1[1] - vect2[1];
141 /*----------------------------------------------------------------------------*/
142 void normalize2d( double vect[2] )
144 double norm = NORM_2D(vect) ;
148 /*----------------------------------------------------------------------------*/
149 void iNormalize2d( int vect[2] )
151 double norm = NORM_2D(vect) ;
152 vect[0] = round( vect[0] / norm ) ;
153 vect[1] = round( vect[1] / norm ) ;
155 /*----------------------------------------------------------------------------*/
156 void crossProduct( const double v1[3], const double v2[3], double res[3] )
158 res[0] = v1[1] * v2[2] - v1[2] * v2[1] ;
159 res[1] = v1[2] * v2[0] - v1[0] * v2[2] ;
160 res[2] = v1[0] * v2[1] - v1[1] * v2[0] ;
162 /*----------------------------------------------------------------------------*/
163 void normalize3D( double vect[3] )
165 double norm = NORM_3D(vect) ;
170 /*----------------------------------------------------------------------------*/
171 void setToIdentity(double mat4D[4][4])
175 for (i = 0; i < 4; i++)
177 for (j = 0; j < 4; j++)
184 /*----------------------------------------------------------------------------*/
185 void mat4DMult(const double mat4D[4][4], const double vect3D[3], double res[3])
189 // w coordinate of the vector is supposed to be 1;
190 for (i = 0; i < 4; i++) {
191 res4D[i] = vect3D[0] * mat4D[i][0] + vect3D[1] * mat4D[i][1]
192 + vect3D[2] * mat4D[i][2] + mat4D[i][3];
194 res[0] = res4D[0] / res4D[3];
195 res[1] = res4D[1] / res4D[3];
196 res[2] = res4D[2] / res4D[3];
198 /*----------------------------------------------------------------------------*/
199 /* check if two values can be considered equal given an accurracy */
200 int safeEqual( double val1, double val2, double accuracy )
202 /* the val1 == val2 is put to avoid division by 0 */
203 return ( val1 == val2 ) || ( Abs( val1 - val2 ) < accuracy * Max( Abs(val1), Abs(val2 ) ) ) ;
205 /*----------------------------------------------------------------------------*/