Files
INS-VN-200/vnproglib/cpp/help/math_2main_8cpp-example.html
r.koeppe 2d22ccd2d6 initial
2024-05-14 02:14:13 +02:00

229 lines
16 KiB
HTML

<!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.10"/>
<title>VectorNav C++ Library: math/main.cpp</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="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>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</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="projectlogo"><img alt="Logo" src="proglib.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">VectorNav C++ Library
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div><!-- top -->
<!-- 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">math/main.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>This example shows the basic math features available with the VectorNav library.</p>
<h1><a class="anchor" id="vsCppMath"></a>
Visual Studio (Windows)</h1>
<ol type="1">
<li>Open the solution file for your specific Visual Studio version located at <code>&lt;root&gt;/cpp/examples/math/projects/vs20XX/math.sln</code>.</li>
<li>Build the entire solution by going to the menu <code>BUILD -&gt; Build Solution</code>.</li>
<li>Right-click the project <code>math</code> and select <code>Debug -&gt; Start new instance</code>.</li>
</ol>
<h1><a class="anchor" id="makeCppMath"></a>
Make (Linux/Mac OS X)</h1>
<ol type="1">
<li>Open a terminal and change to the directory <code>&lt;root&gt;/cpp/examples/math</code> .</li>
<li>To build the example, run the command <code>make</code> .</li>
<li>Run the example by executing the command <code>./math</code> .</li>
</ol>
<h1><a class="anchor" id="ClionCppMath"></a>
CLion (Windows/Linux/Mac OS X)</h1>
<ol type="1">
<li>Open the project file located at <code>&lt;root&gt;/cpp/examples/math</code> in CLion.</li>
<li>Make sure the <code>math</code> configuration is active. You can set this by clicking the small drop-down list in the upper-right corner and selecting the option <code>math</code>.</li>
<li>Build the solution by going to the menu <code>Run -&gt; Build</code>.</li>
<li>Run the example by going to the menu <code>Run -&gt; Run 'math'</code>.</li>
</ol>
<h1><a class="anchor" id="otherCppMath"></a>
Other</h1>
<p>To compile and run for an environment not listed here, you will need to add all of the <code>*.c</code> files in the directory <code>&lt;root&gt;/c/src</code> along with the file located at <code>&lt;root&gt;/cpp/examples/math/main.cpp</code> to your project for compilation. You will also need to add <code>&lt;root&gt;/cpp/include</code> to your include directories.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="vector_8h.html">vn/vector.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="matrix_8h.html">vn/matrix.h</a>&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacevn_1_1math.html">vn::math</a>;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// The VectorNav Programming Library provides structures for representing vectors and matrices.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// Common vectors you will normally encounter are conveniently named. Below we create some simple vector objects.</span></div>
<div class="line"></div>
<div class="line"> <a name="_a0"></a><a class="code" href="structvn_1_1math_1_1vec.html">vec3f</a> v1(1.0f, 2.0f, 3.0f); <span class="comment">// An initialized 3 component vector of floats.</span></div>
<div class="line"> <a name="_a1"></a><a class="code" href="structvn_1_1math_1_1vec.html">vec3d</a> v2(4.0, 5.0, 6.0); <span class="comment">// An initialized 3 component vector of doubles.</span></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1vec.html">vec4f</a> v3(1.0f, 2.0f, 3.0f, 4.0f); <span class="comment">// An initialized 4 component vector of floats.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// Convenience methods are provided to display vector values.</span></div>
<div class="line"></div>
<div class="line"> <span class="keywordtype">string</span> v1Str = str(v1);</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;v1: &quot;</span> &lt;&lt; v1Str &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Or you can just write directly to cout.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;v2: &quot;</span> &lt;&lt; v2 &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;v3: &quot;</span> &lt;&lt; v3 &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Common vector values are readily available.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;zero vector: &quot;</span> &lt;&lt; vec3f::zero() &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;x-direction vector: &quot;</span> &lt;&lt; vec3f::unitX() &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// A variety of vector operations are available.</span></div>
<div class="line"></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1vec.html">vec3f</a> va(1, 2, 3);</div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1vec.html">vec3f</a> vb(4, 5, 6);</div>
<div class="line"></div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;va: &quot;</span> &lt;&lt; va &lt;&lt; std::endl;</div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;vb: &quot;</span> &lt;&lt; vb &lt;&lt; std::endl;</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va + vb = &quot;</span> &lt;&lt; va + vb &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va - vb = &quot;</span> &lt;&lt; va - vb &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;2 * va = &quot;</span> &lt;&lt; 2.f * va &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va / 3 = &quot;</span> &lt;&lt; va / 3.f &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;-va = &quot;</span> &lt;&lt; -va &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va magnitude = &quot;</span> &lt;&lt; va.<a name="a2"></a><a class="code" href="structvn_1_1math_1_1vec.html#a3d62ee03c5842e16a67b62e7c4c11ecc">mag</a>() &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va normalized = &quot;</span> &lt;&lt; va.<a name="a3"></a><a class="code" href="structvn_1_1math_1_1vec.html#a12ee38e4566aa64fe2dca3f72ff8e9ac">norm</a>() &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// You can also access the individual vector components in a variety of ways.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va y-component: &quot;</span> &lt;&lt; va.y &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;va z-component: &quot;</span> &lt;&lt; va[2] &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Although you will probably use the conveniently named vectors, sometime you will need a more specialized version.</span></div>
<div class="line"> <span class="comment">// Because the vectors are based on a template structure, you can easily create the type you need.</span></div>
<div class="line"></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1vec.html">vec&lt;5, unsigned short&gt;</a> v4(7); <span class="comment">// 5 component vector of unsigned shorts, all components initialized to the value of 7.</span></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1vec.html">vec&lt;10, bool&gt;</a> v5(<span class="keyword">true</span>); <span class="comment">// 10 component vector of bools, all components initialized to the value of true.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;v4: &quot;</span> &lt;&lt; v4 &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;v5: &quot;</span> &lt;&lt; v5 &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// You may often find yourself working with matrices so we show some examples using the provided matrix structures.</span></div>
<div class="line"></div>
<div class="line"> <a name="_a4"></a><a class="code" href="structvn_1_1math_1_1mat.html">mat3f</a> m1(1, 2, 3, 4, 5, 6, 7, 8, 9); <span class="comment">// 3x3 matrix of floats.</span></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1mat.html">mat2d</a> m2(1, 2, 3, 4); <span class="comment">// 2x2 matrix of doubles.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// As with vectors, there are convenient string functions.</span></div>
<div class="line"></div>
<div class="line"> <span class="keywordtype">string</span> m1Str = str(m1);</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;m1: &quot;</span> &lt;&lt; m1Str &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Again, you can just write the matrix out to cout.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;m2: &quot;</span> &lt;&lt; m2 &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Common matrix values are readily available.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;one 3x3 matrix: &quot;</span> &lt;&lt; mat3f::one() &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;identity 3x3 matrix: &quot;</span> &lt;&lt; mat3f::identity() &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// A variety of matrix operations are available.</span></div>
<div class="line"></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1mat.html">mat3f</a> ma(1, 2, 3, 4, 5, 6, 7, 8, 9);</div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1mat.html">mat3f</a> mb = mat3f::one();</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;ma + mb = &quot;</span> &lt;&lt; ma + mb &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;ma - mb = &quot;</span> &lt;&lt; ma - mb &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;2 * ma = &quot;</span> &lt;&lt; 2.f * ma &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;ma / 3 = &quot;</span> &lt;&lt; ma / 3.f &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;-ma = &quot;</span> &lt;&lt; -ma &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;ma transpose = &quot;</span> &lt;&lt; ma.<a name="a5"></a><a class="code" href="structvn_1_1math_1_1mat.html#ad787d0b22c792da3964b34bc8b1ace60">transpose</a>() &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Access to the individual matrix elements is illustrated below.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;0,0 element of ma: &quot;</span> &lt;&lt; ma.e00 &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;0,1 element of ma: &quot;</span> &lt;&lt; ma(0,1) &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// You can also create different sizes and underlying data types for the matrices because of templating.</span></div>
<div class="line"></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1mat.html">mat&lt;2, 3, short&gt;</a> m4(7); <span class="comment">// 2x3 matrix of shorts, all elements initialized to the value of 7.</span></div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1mat.html">mat&lt;3, 7, bool&gt;</a> m5(<span class="keyword">true</span>); <span class="comment">// 3x7 matrix of bools, all elements initialized to the value of true.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;m4: &quot;</span> &lt;&lt; m4 &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;m5: &quot;</span> &lt;&lt; m5 &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Tue Jan 3 2017 18:27:03 for VectorNav C++ Library by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.10
</small></address>
</body>
</html>