Maths - sfvec3f C++ files

related files

h file

   /*Title: mjbWorld
   Copyright (c) 1998-2007 Martin John Baker 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.For information about the GNU General public: License see http://www.gnu.org/To discuss this program http://sourceforge.net/forum/forum.php?forum_id=122133
   also see website https://www.euclideanspace.com/
   *//*
   for theory see:
   https://www.euclideanspace.com/maths/vectors/index.htm
   */__gc class sfvec3f : public property {
   public: static bool saveAsDouble = false ;
   public: double x;
   public: double y;
   public: double z; // VRML only supports float but allow override if higher resolution required public: sfvec3f(double x,double y,double z); public: sfvec3f(sfvec3f* in); public: sfvec3f(); public: void sub(sfvec3f* other);
   public: void add(sfvec3f* other);
   public: void normalize();
   public: void scale(double sc);
   public: double distanceSquared(sfvec3f* other);
   void cross(sfvec3f* other);
   double dot(sfvec3f* other); static sfvec3f* sub(sfvec3f* a,sfvec3f* b){
   return new sfvec3f(a->x - b->x,a->y - b->y,a->z - b->z);
   } public: String* vrmlType(); public: static String* vrmlType_s(){
   return "SFVec3f";
   } public: String* ToString(); public: property* clone(); /** create an array of the appropriate type
   * with a size given by the parameter
   */
   public: property* createArray(int size)[]; public: void setX(double v); public: void setY(double v); public: void setZ(double v); public: double getx(); public: double gety(); public: double getz(); public: sfvec3f* minus(); public: String* toStatic(); /* if i=0 take minimum otherwise maximum */
   public: void combine(sfvec3f* bb_current,int i); public: bool greaterThan(sfvec3f* other); public: bool lessThan(sfvec3f* other);
   public: bool Equals(sfvec3f* v2);
   /** output as a string
   * mode values
   * 0 - output modified values
   * 1 - output original values
   * 2 - output attribute
   * 3 - output attribute in brackets
   * 4 - output with f prefix
   */
   public: String* outstring(int format);/** write to file
   * filter = information about output
   * mode values
   * 0 - output VRML97 modified values
   * 1 - output VRML97 original values
   * 2 - output xml (x3d)
   * 3 - output attribute in brackets
   * 4 - output with f prefix
   */
   public: void write(filter* f,int mode,int indent); public: bool readX3d(filter* f); /** used by mfparam.vrml2par
   */
   public: bool instring(filter* f,sfparam* sfp,nodeBean* n,int mode) ; /** used by mfvec3f.instring
   */
   public: bool instring(filter* f,String* s1); public: static Type* getEditClass(){
   return __typeof(sfvec3fEditor);
   }
   };
 

CPP file

/*Title: mjbWorld
   Copyright (c) 1998-2002 Martin John BakerThis program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General 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 License for more details.For information about the GNU General License see http://www.gnu.org/To discuss this program http://sourceforge.net/forum/forum.php?forum_id=122133
   also see website https://www.euclideanspace.com/
   *//*
   for theory see:
   https://www.euclideanspace.com/maths/vectors/index.htm
   */#include "mjbModel.h"
   /* x3d definition<!ENTITY % SFVec3f "CDATA"> <!-- ArrayList3Float -->
   */
   //private sfvec3fEditor editor = null; // VRML only supports float but allow override if higher resolution requiredsfvec3f::sfvec3f(double x1,double y1,double z1) {
   x=x1;
   y=y1;
   z=z1;
   saveAsDouble = false ;
   }sfvec3f::sfvec3f(sfvec3f* in) { // copy constructor
   x=(in) ? in->x : 0;
   y= (in) ? in->y : 0;
   z= (in) ? in->z : 1;
   saveAsDouble = false ;
   }sfvec3f::sfvec3f() {
   saveAsDouble = false ;
   }String* sfvec3f::vrmlType(){
   return "SFVec3f";
   }property* sfvec3f::clone() {
   //Console::WriteLine("sfvec3f->clone");
   return new sfvec3f(this);
   }/*
   for theory see:
   https://www.euclideanspace.com/maths/vectors/index.htm
   */
   void sfvec3f::sub(sfvec3f* other){
   x-= other->x;
   y-= other->y;
   z-= other->z;
   }/*
   for theory see:
   https://www.euclideanspace.com/maths/vectors/index.htm
   */
   void sfvec3f::add(sfvec3f* other){
   x+= other->x;
   y+= other->y;
   z+= other->z;
   }/*
   for theory see:
   https://www.euclideanspace.com/maths/vectors/normals/index.htm
   */
   void sfvec3f::normalize(){
   double t = Math::Sqrt(x*x + y*y + z*z);
   x /= t;
   y /= t;
   z /= t;
   }void sfvec3f::scale(double sc){
   x *= sc;
   y *= sc;
   z *= sc;
   }
   
   double sfvec3f::distanceSquared(sfvec3f* other){
   double x1 = other->x - x;
   double y1 = other->y - y;
   double z1 = other->z - z;
   return x1*x1 + y1*y1 + z1*z1;
   }void sfvec3f::cross(sfvec3f* other) {
   double xh = y * other->z - other->y * z;
   double yh = z * other->x - other->z * x;
   double zh = x * other->y - other->x * y;
   x = xh;
   y = yh;
   z = zh;
   }double sfvec3f::dot(sfvec3f* other) {
   return (x * other->x) + (y * other->y) + (z * other->z);
   }/** create an array of the appropriate type
   * with a size given by the parameter
   */
   property* sfvec3f::createArray(int size)[]{
   return new sfvec3f*[size];
   }void sfvec3f::setX(double v) {
   x=v;
   }void sfvec3f::setY(double v) {
   y=v;
   }void sfvec3f::setZ(double v) {
   z=v;
   }double sfvec3f::getx() {
   return x;
   }double sfvec3f::gety() {
   return y;
   }double sfvec3f::getz() {
   return z;
   }sfvec3f* sfvec3f::minus() {
   return new sfvec3f(-x,-y,-z);
   }String* sfvec3f::toStatic() {
   return String::Concat(__box(x)->ToString(),S"f,",
   __box(y)->ToString(),S"f,",
   __box(z)->ToString(),S"f");
   }String* sfvec3f::ToString() {
   return String::Concat(S"(",__box(x)->ToString(),S",",
   __box(y)->ToString(),S",",
   __box(z)->ToString(),S")");
   }bool sfvec3f::greaterThan(sfvec3f* other) {
   if (other->x > x) return true;
   if (other->y > y) return true;
   if (other->z > z) return true;
   return false;
   }bool sfvec3f::lessThan(sfvec3f* other) {
   if (other->x < x) return true;
   if (other->y < y) return true;
   if (other->z < z) return true;
   return false;
   }bool sfvec3f::Equals(sfvec3f* v2) {
   if (!v2) return false;
   if (x!= v2->x) return false;
   if (y!= v2->y) return false;
   if (z!= v2->z) return false;
   return true;
   }/** use to combine bounds
   * if i=0 take minimum otherwise maximum */
   void sfvec3f::combine(sfvec3f* other,int i){
   if (i==0) { // take minimum
   if (other->x < x) x=other->x;
   if (other->y < y) y=other->y;
   if (other->z < z) z=other->z;
   } else { // take maximum
   if (other->x > x) x=other->x;
   if (other->y > y) y=other->y;
   if (other->z > z) z=other->z;
   }
   }/** output as a string
   * mode values
   * 0 - output modified values
   * 1 - output original values
   * 2 - output attribute
   * 3 - output attribute in brackets
   * 4 - output with f prefix
   */
   String* sfvec3f::outstring(int format) {
   if (format == 3) {
   if (saveAsDouble)
   return String::Concat(S"(",__box(x)->ToString(),S" ",
   __box(y)->ToString(),S" ",
   __box(z)->ToString(),S")");
   else
   return String::Concat(S"(",__box((float)x)->ToString(),S"    ",
   __box((float)y)->ToString(),S" ",
   __box((float)z)->ToString(),S")");
   } else if (format == 4) {
   return String::Concat(__box((float)x)->ToString(),S"f,",
   __box((float)y)->ToString() , S"f," ,
   __box((float)z)->ToString() , S"f");
   } else {
   if (saveAsDouble)
   return String::Concat(__box(x)->ToString(),S" ",
   __box(y)->ToString(),S" ",
   __box(z)->ToString());
   else
   return String::Concat(__box((float)x)->ToString(),S" ",
   __box((float)y)->ToString(),S" ",
   __box((float)z)->ToString());
   }
   }/** write to file
   * filter = information about output
   * mode values
   * 0 - output VRML97 modified values
   * 1 - output VRML97 original values
   * 2 - output xml (x3d)
   * 3 - output attribute in brackets
   * 4 - output with f prefix
   */
   void sfvec3f::write(filter* f,int mode,int indent){
   if (mode == 3) {
   if (saveAsDouble) {
   f->write(String::Concat(S"(",__box(x)->ToString(),S" ",
   __box(y)->ToString(),S" ",
   __box(z)->ToString(),S")"));
   return;
   } else {
   f->write(String::Concat(S"(",__box((float)x)->ToString(),S"    ",
   __box((float)y)->ToString(),S" ",
   __box((float)z)->ToString(),S")"));
   return;
   }
   } else if (mode == 4) {
   f->write(String::Concat(__box(x)->ToString(),S"f,",
   __box(y)->ToString(),S"f,",
   __box(z)->ToString(),S"f"));
   return;
   } else {
   if (saveAsDouble) {
   f->write(String::Concat(__box(x)->ToString(),S" ",
   __box(y)->ToString(),S" ",
   __box(z)->ToString()));
   return;
   } else
   f->write(String::Concat(__box((float)x)->ToString(),S" ",
   __box((float)y)->ToString(),S" ",
   __box((float)z)->ToString()));
   }
   // f->write(outstring(mode));
   }bool sfvec3f::readX3d(filter* f) {
   String* s;
   s=f->nextToken();
   if (s->Equals("=")) {
   s=f->nextToken();
   if (s->Equals("'")) {
   s=f->nextToken();
   if (s->Equals("(")) {
   s=f->nextToken();
   }
   }
   }
   x = Single::Parse(s);
   s=f->nextToken();
   y = Single::Parse(s);
   s=f->nextToken();
   z = Single::Parse(s);
   return true;
   }/** used by mfparam->vrml2par
   */
   bool sfvec3f::instring(filter* f,sfparam* sfp,nodeBean* n,int mode) {
   String* s;
   s=f->nextToken();
   if (s) if (s->Equals("IS")) {
   s=f->nextToken();
   if (sfp) sfp->setIs(s);
   return true;
   }
   x = Single::Parse(s);
   s=f->nextToken();
   y = Single::Parse(s);
   s=f->nextToken();
   z = Single::Parse(s);
   return true;
   }/** used by mfvec3f->instring
   */
   bool sfvec3f::instring(filter* f,String* s1) {
   String* s;
   x = Single::Parse(s1);
   s=f->nextToken();
   y = Single::Parse(s);
   s=f->nextToken();
   z = Single::Parse(s);
   return true;
   }

metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Mathematics for 3D game Programming - Includes introduction to Vectors, Matrices, Transforms and Trigonometry. (But no euler angles or quaternions). Also includes ray tracing and some linear & rotational physics also collision detection (but not collision response).

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.