1 /* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License as 4 * published by the Free Software Foundation; either version 2 of the 5 * License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, but 8 * WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOUSE. See the GNU 10 * General Public License for more details. 11 * 12 * You should have recieved a copy of the GNU General Public License 13 * along with this program; if not write to the Free Software 14 * Foundation, inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 15 * USA 16 */ 17 18 package j3d.functions; 19 20 import j3d._NurbCurve; 21 import j3d._Point; 22 import util.VectorFunction; 23 24 /** 25 * @author Jonas Forssell 26 * 27 * 28 */ 29 public class DirectedDistancePointToCurveFunction implements VectorFunction { 30 31 _NurbCurve c1; 32 _Point p1,p2,p; 33 float dist; 34 DirectedDistancePointToCurveFunction(_Point p, _NurbCurve c1)35 public DirectedDistancePointToCurveFunction (_Point p, _NurbCurve c1) { 36 this.p = p; 37 this.c1 = c1; 38 p2 = new _Point(0,0,0); 39 } 40 41 /** 42 * Compute the distance between two nurb curves at given parameter value. If the 43 * value is out of bounds, a large value is returned to create a border. 44 * 45 * @param x parameter input vector. Always size 2 (two curves). 46 * 47 * @return Distance squared between the curves at given points. 48 */ f(float[] x)49 public float f (float[] x) { 50 51 if (x[0] < 0 || x[0] > 1) return 1E9f; 52 53 p1 = c1.getPointAt(x[0],false); 54 55 p2.x = p.x + p.vx*x[1]; 56 p2.y = p.y + p.vy*x[1]; 57 p2.z = p.z + p.vz*x[1]; 58 59 return (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) + (p1.z-p2.z)*(p1.z-p2.z); 60 61 } 62 63 } 64