<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.12"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>DFNgen: vectorFunctions.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">DFNgen
   &#160;<span id="projectnumber">2.0</span>
   </div>
   <div id="projectbrief">DFN Model Generator</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.12 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('vector_functions_8cpp_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">vectorFunctions.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="vector_functions_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#include &lt;cmath&gt;</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="structures_8h.html">structures.h</a>&quot;</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="vector_functions_8h.html">vectorFunctions.h</a>&quot;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#include &lt;cmath&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="input_8h.html">input.h</a>&quot;</span><span class="comment">//eps</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment">//see vectorFunctions.h for template function implimentations</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment">//such as crossProduct, dorProduct, magnitude,...</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment">/************************************************************/</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment">/************************************************************/</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment">// Projection funcion without sqrt()</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment">// Projects v1 onto v2</span></div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment">// Arg 1: Pointer to vector 1 containing {x,y,z}, all doubles</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment">// Arg 2: Pointer to vector 2 containing {x,y,z}, all doubles</span></div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment">// Return: Resulting vector. </span></div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="comment">//         Must delete return array manually. Created with new.</span></div><div class="line"><a name="l00020"></a><span class="lineno"><a class="line" href="vector_functions_8h.html#a3c2b95a1658779bf4d2b104fa9513e75">   20</a></span>&#160;<span class="keywordtype">double</span> *<a class="code" href="vector_functions_8cpp.html#a3c2b95a1658779bf4d2b104fa9513e75">projection</a>( <span class="keyword">const</span> <span class="keywordtype">double</span> *v1, <span class="keyword">const</span> <span class="keywordtype">double</span> *v2 ) {</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;              </div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    <span class="keywordtype">double</span> v2_ls = v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2];</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;    </div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    <span class="keywordtype">double</span> *result = <span class="keyword">new</span> <span class="keywordtype">double</span>[3];</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;    </div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;    <span class="keywordflow">if</span> (v2_ls &lt; <a class="code" href="_d_f_nmain_8cpp.html#a974b448c3d0a13d483d36ef15de369b5">eps</a>) {</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;        result[0] = 0;</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;        result[1] = 0;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        result[2] = 0;              </div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;    }</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;    <span class="keywordflow">else</span> {</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;        <span class="keywordtype">double</span> temp = ((v2[0]*v1[0] + v2[1]*v1[1] + v2[2]*v1[2] )/v2_ls );</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;        result[0] =  v2[0] * temp;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;        result[1] =  v2[1] * temp;</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;        result[2] =  v2[2] * temp;</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;    }</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    <span class="keywordflow">return</span> result;</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;}</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;<span class="comment">/************************************************************/</span></div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;<span class="comment">/************************************************************/</span></div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;<span class="comment">// Calculates the distance between two points in 3D space.</span></div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;<span class="comment">// Arg 1: Pointer to 3 element double array {x,y,z}</span></div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;<span class="comment">// Arg 2: Pointer to 3 element double array {x,y,z}</span></div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;<span class="comment">// Return: Distance between point A and point B</span></div><div class="line"><a name="l00046"></a><span class="lineno"><a class="line" href="vector_functions_8h.html#af04ebdcbe44a303101290e88a5971c71">   46</a></span>&#160;<span class="keywordtype">double</span> <a class="code" href="vector_functions_8cpp.html#af04ebdcbe44a303101290e88a5971c71">euclideanDistance</a>(<span class="keywordtype">double</span> *A, <span class="keywordtype">double</span> *B){</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;        <span class="keywordtype">double</span> temp1 = A[0] - B[0];</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;        <span class="keywordtype">double</span> temp2 = A[1] - B[1];</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;        <span class="keywordtype">double</span> temp3 = A[2] - B[2];</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;        temp1 = temp1*temp1;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;        temp2 = temp2*temp2;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;        temp3 = temp3*temp3;</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;        <span class="keywordflow">return</span> std::sqrt(temp1+temp2+temp3);</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;}</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;</div><div class="line"><a name="l00057"></a><span class="lineno"><a class="line" href="vector_functions_8h.html#ac02fbd874b60abb201dce0afd7775416">   57</a></span>&#160;<span class="keywordtype">double</span> <a class="code" href="vector_functions_8cpp.html#af04ebdcbe44a303101290e88a5971c71">euclideanDistance</a>(<a class="code" href="struct_point.html">Point</a> &amp;A, <a class="code" href="struct_point.html">Point</a> &amp;B) {</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;        <span class="keywordtype">double</span> temp1 = A.<a class="code" href="struct_point.html#ab99c56589bc8ad5fa5071387110a5bc7">x</a> - B.<a class="code" href="struct_point.html#ab99c56589bc8ad5fa5071387110a5bc7">x</a>;</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;        <span class="keywordtype">double</span> temp2 = A.<a class="code" href="struct_point.html#afa38be143ae800e6ad69ce8ed4df62d8">y</a> - B.<a class="code" href="struct_point.html#afa38be143ae800e6ad69ce8ed4df62d8">y</a>;</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;        <span class="keywordtype">double</span> temp3 = A.<a class="code" href="struct_point.html#a05ba3b1dfcb19430582ae953cbbfbded">z</a> - B.<a class="code" href="struct_point.html#a05ba3b1dfcb19430582ae953cbbfbded">z</a>;</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;        temp1 = temp1*temp1;</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;        temp2 = temp2*temp2;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;        temp3 = temp3*temp3;</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;        <span class="keywordflow">return</span> std::sqrt(temp1+temp2+temp3);</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;}</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;<span class="comment">/************************************************************/</span></div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;<span class="comment">/************************************************************/</span></div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;<span class="comment">// Claculates the angle between two vectors</span></div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;<span class="comment">// Arg 1: Pointer to 3 element double array, {x,y,z} vector</span></div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;<span class="comment">// Arg 2: Pointer to 3 element double array, {x,y,z} vector</span></div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;<span class="comment">// Return: Angle between both vectors in radians</span></div><div class="line"><a name="l00074"></a><span class="lineno"><a class="line" href="vector_functions_8h.html#afd59d451cff22898e4153261bc1fb38e">   74</a></span>&#160;<span class="keywordtype">double</span> <a class="code" href="vector_functions_8cpp.html#afd59d451cff22898e4153261bc1fb38e">angleBeteenVectors</a>(<span class="keyword">const</span> <span class="keywordtype">double</span> *vector1,<span class="keyword">const</span> <span class="keywordtype">double</span> *vector2) {</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    <span class="comment">// cos(x) = u.v / (mag(u) * mag(v))</span></div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;    <span class="comment">// x = arccos(x) = (mag(u) * mag(v)) / u.v</span></div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;    <span class="keywordtype">double</span> dot = <a class="code" href="vector_functions_8h.html#abb1d4beb5939c7a5ddd4978c15fab7bb">dotProduct</a>(vector1, vector2); </div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;    <span class="keywordtype">double</span> v1Mag = <a class="code" href="vector_functions_8h.html#a92da2058f3ed2a6db16d2fe0e445794a">magnitude</a>(vector1[0], vector1[1], vector1[2]);</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    <span class="keywordtype">double</span> v2Mag = <a class="code" href="vector_functions_8h.html#a92da2058f3ed2a6db16d2fe0e445794a">magnitude</a>(vector2[0], vector2[1], vector2[2]);</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;    <span class="keywordtype">double</span> angle = std::acos(dot/(v1Mag*v2Mag));</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;    <span class="keywordflow">return</span> angle;</div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;}</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;    </div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;</div><div class="ttc" id="vector_functions_8cpp_html_af04ebdcbe44a303101290e88a5971c71"><div class="ttname"><a href="vector_functions_8cpp.html#af04ebdcbe44a303101290e88a5971c71">euclideanDistance</a></div><div class="ttdeci">double euclideanDistance(double *A, double *B)</div><div class="ttdef"><b>Definition:</b> <a href="vector_functions_8cpp_source.html#l00046">vectorFunctions.cpp:46</a></div></div>
<div class="ttc" id="vector_functions_8h_html_a92da2058f3ed2a6db16d2fe0e445794a"><div class="ttname"><a href="vector_functions_8h.html#a92da2058f3ed2a6db16d2fe0e445794a">magnitude</a></div><div class="ttdeci">T magnitude(T x, T y, T z)</div><div class="ttdef"><b>Definition:</b> <a href="vector_functions_8h_source.html#l00086">vectorFunctions.h:86</a></div></div>
<div class="ttc" id="input_8h_html"><div class="ttname"><a href="input_8h.html">input.h</a></div></div>
<div class="ttc" id="vector_functions_8cpp_html_afd59d451cff22898e4153261bc1fb38e"><div class="ttname"><a href="vector_functions_8cpp.html#afd59d451cff22898e4153261bc1fb38e">angleBeteenVectors</a></div><div class="ttdeci">double angleBeteenVectors(const double *vector1, const double *vector2)</div><div class="ttdef"><b>Definition:</b> <a href="vector_functions_8cpp_source.html#l00074">vectorFunctions.cpp:74</a></div></div>
<div class="ttc" id="_d_f_nmain_8cpp_html_a974b448c3d0a13d483d36ef15de369b5"><div class="ttname"><a href="_d_f_nmain_8cpp.html#a974b448c3d0a13d483d36ef15de369b5">eps</a></div><div class="ttdeci">double eps</div><div class="ttdef"><b>Definition:</b> <a href="_d_f_nmain_8cpp_source.html#l00039">DFNmain.cpp:39</a></div></div>
<div class="ttc" id="struct_point_html"><div class="ttname"><a href="struct_point.html">Point</a></div><div class="ttdef"><b>Definition:</b> <a href="structures_8h_source.html#l00111">structures.h:111</a></div></div>
<div class="ttc" id="struct_point_html_afa38be143ae800e6ad69ce8ed4df62d8"><div class="ttname"><a href="struct_point.html#afa38be143ae800e6ad69ce8ed4df62d8">Point::y</a></div><div class="ttdeci">double y</div><div class="ttdef"><b>Definition:</b> <a href="structures_8h_source.html#l00113">structures.h:113</a></div></div>
<div class="ttc" id="vector_functions_8h_html"><div class="ttname"><a href="vector_functions_8h.html">vectorFunctions.h</a></div></div>
<div class="ttc" id="structures_8h_html"><div class="ttname"><a href="structures_8h.html">structures.h</a></div></div>
<div class="ttc" id="struct_point_html_ab99c56589bc8ad5fa5071387110a5bc7"><div class="ttname"><a href="struct_point.html#ab99c56589bc8ad5fa5071387110a5bc7">Point::x</a></div><div class="ttdeci">double x</div><div class="ttdef"><b>Definition:</b> <a href="structures_8h_source.html#l00112">structures.h:112</a></div></div>
<div class="ttc" id="vector_functions_8cpp_html_a3c2b95a1658779bf4d2b104fa9513e75"><div class="ttname"><a href="vector_functions_8cpp.html#a3c2b95a1658779bf4d2b104fa9513e75">projection</a></div><div class="ttdeci">double * projection(const double *v1, const double *v2)</div><div class="ttdef"><b>Definition:</b> <a href="vector_functions_8cpp_source.html#l00020">vectorFunctions.cpp:20</a></div></div>
<div class="ttc" id="vector_functions_8h_html_abb1d4beb5939c7a5ddd4978c15fab7bb"><div class="ttname"><a href="vector_functions_8h.html#abb1d4beb5939c7a5ddd4978c15fab7bb">dotProduct</a></div><div class="ttdeci">T dotProduct(const T *A, const T *B)</div><div class="ttdef"><b>Definition:</b> <a href="vector_functions_8h_source.html#l00058">vectorFunctions.h:58</a></div></div>
<div class="ttc" id="struct_point_html_a05ba3b1dfcb19430582ae953cbbfbded"><div class="ttname"><a href="struct_point.html#a05ba3b1dfcb19430582ae953cbbfbded">Point::z</a></div><div class="ttdeci">double z</div><div class="ttdef"><b>Definition:</b> <a href="structures_8h_source.html#l00114">structures.h:114</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="vector_functions_8cpp.html">vectorFunctions.cpp</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.12 </li>
  </ul>
</div>
</body>
</html>
