(*
  skeg - Sex, Kinematics, Elegance and Glory.
  Copyright (C) 2004 David Baelde and Samuel Mimram.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
*)


(* $Id: vect.ml,v 1.1 2004/04/19 19:11:13 smimou Exp $ *)

(** Operations on vectors.

@author David Baelde, Samuel Mimram *)



(** Type of vectors. *)

type vect = float * float * float

let proj1 (x, _, _) = x

let proj2 (_, y, _) = y

let proj3 (_, _, z) = z

(** Get the square of the norm. *)

let sqnorm (x, y, z) =
  x *. x +. y *. y +. z *. z

(** Get the norm. *)

let norm v =
  sqrt (sqnorm v)

let mult (x, y, z) a =
  (x *. a, y *. a, z *. a)

let div (x, y, z) a =
  (x /. a, y /. a, z /. a)

(** Normalize. *)

let normalize v =
  div v (norm v)

(** Scalar product. *)

let scal (x1, y1, z1) (x2, y2, z2) =
  x1 *. x2 +. y1 *. y2 +. z1 *. z2

(** add v1 v2 returns v1 + v2. *)

let add (x1, y1, z1) (x2, y2, z2) =
  (x1 +. x2, y1 +. y2, z1 +. z2)

(** sub v1 v2 returns v1 - v2. *)

let sub (x1, y1, z1) (x2, y2, z2) =
  (x1 -. x2, y1 -. y2, z1 -. z2)

(** dist_from_point o v p gives the disance between the point p and the line o + k * v. *)

let dist_from_point o v p =
  let v' = sub p o in
  let s = scal v' v in
    sqrt ((sqnorm v') -. s *. s)