JNR
laLine3.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laLine3.cpp
32 // Class, representing a three-dimensional line
33 //
34 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
35 //
36 // NOTE: This implementation has been imported from
37 // the project Old-projects\Venomous3DEngine_work\current\3DENG
38 // It has not been tested (NT), so any usage should be made with caution
39 
40 
41 #include "stdafx.h"
42 #include "Core.h"
43 
44 // TODO: NT (Not Tested)
45 // Get the point of crossing with another laLine3
46 // NOTE: This code needs major reworking because it is very error-prone
47 //
48 laPoint3 laLine3::intersection(laLine3 &l, double &pk1)
49 {
50  double k1;
51  double btm;
52 
53  //yz
54  btm = vector.z()*l.vector.y() - vector.y()*l.vector.z();
55  if((btm)!=0){
56  k1 = -( vector.y()*origin.z() + vector.z()*l.origin.y() - vector.z()*origin.y() - vector.y()*l.origin.z() )/
57  ( btm );
58  pk1 = k1;
59 
60  laPoint3 pt; l.at(k1, &pt);
61  return pt;
62  }
63 
64  //xz
65  btm = vector.z()*l.vector.x() - vector.x()*l.vector.z();
66  if((btm)!=0){
67  k1 = -( vector.x()*origin.z() + vector.z()*l.origin.x() - vector.z()*origin.x() - vector.x()*l.origin.z() )/
68  ( btm );
69  pk1 = k1;
70 
71  laPoint3 pt; l.at(k1, &pt);
72  return pt;
73  }
74 
75  //xy;
76  btm = vector.y()*l.vector.x() - vector.x()*l.vector.y();
77  if((btm)!=0){
78  k1 = -( vector.x()*origin.y() + vector.y()*l.origin.x() - vector.y()*origin.x() - vector.x()*l.origin.y() )/
79  ( btm );
80  pk1 = k1;
81 
82  laPoint3 pt; l.at(k1, &pt);
83  return pt;
84  }
85 
86  k1 = 999; //wtf??
87  pk1 = k1;
88 
89  laPoint3 pt; l.at(k1, &pt);
90  return pt;
91 }
92 
93 // TODO: NT (Not Tested)
94 // Get the point of crossing with a parametric plane in 3D
95 //
96 laPoint3 laLine3::intersection(laPlane3 &pn, double *pk)
97 {
98  double k = -1*(pn.d + pn.normal.dot(origin)) / pn.normal.dot( vector );
99 
100  //double k = -(pn.d + pn.normal.x()*origin.x() + pn.normal.y()*origin.y() + pn.normal.z()*origin.z()) /
101  // (pn.normal.x()*vector.x() + pn.normal.y()*vector.y() + pn.normal.z()*vector.z());
102 
103  if( pk!=NULL ) *pk = k;
104 
105  laPoint3 ptIntersection;
106  at(k, &ptIntersection);
107 
108  return ptIntersection;
109 }
110 
111 /*void GetPnCross(laPoint3 &p, const laPlane3 &pn, double *pk=NULL)
112 {
113  double k = -(pn.d+pn.a()*org.x()+pn.b()*org.y()+pn.c*org.z())/
114  (pn.a()*dir.x() + pn.b()*dir.y() + pn.c*dir.z() );
115  p[0] = org.x() + k*dir.x();
116  p[1] = org.y() + k*dir.y();
117  p[2] = org.z() + k*dir.z();
118 
119  double e=pn.a()*p.x() + pn.b()*p.y() + pn.c*p.z() + pn.d;
120  if(pk!=NULL)*pk = k;
121 }*/
122 
123 void laLine3::draw(class laRenderer* r, laPoint3 pos)
124 {
125  r->drawLine(pos + origin, pos + end());
126 }
laPoint3 intersection(laLine3 &l, double &pk1)
Find point of intersection (or nearest point) to another laLine3.
Definition: laLine3.cpp:48
3D line segment
Definition: laLine3.h:40
double d
Distance from the origin of the coordinate system.
Definition: laPlane3.h:42
laPoint3 vector
Vector ( not normalized, so k = [0;1] traces the whole line segment )
Definition: laLine3.h:44
laPoint3 end() const
Get endpoint.
Definition: laLine3.h:82
laPoint3 origin
Point of origin.
Definition: laLine3.h:43
laPoint3 normal
Normal vector.
Definition: laPlane3.h:43
void draw(class laRenderer *r, laPoint3 pos)
Draw the line ( primarily for debug purposes )
Definition: laLine3.cpp:123
void at(double k, laPoint3 *ppt) const
Get point at 1/k lenght (returns in user-specified pointer)
Definition: laLine3.h:85
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
3D plane
Definition: laPlane3.h:39