mirror of
https://github.com/boostorg/geometry.git
synced 2025-05-11 21:44:04 +00:00
Added example to work with units
[SVN r67570]
This commit is contained in:
parent
4e019a7227
commit
7adc8b2d06
207
example/08_units_example.cpp
Normal file
207
example/08_units_example.cpp
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||||
|
//
|
||||||
|
// Copyright Barend Gehrels 2011, Geodan, Amsterdam, the Netherlands
|
||||||
|
// Use, modification and distribution is subject to the Boost Software License,
|
||||||
|
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// Example combining Boost.Geometry with Boost.Units
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <boost/geometry/geometry.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
#include <boost/units/quantity.hpp>
|
||||||
|
#include <boost/units/systems/si/length.hpp>
|
||||||
|
#include <boost/units/systems/cgs/length.hpp>
|
||||||
|
#include <boost/units/systems/si/io.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
// TEMPORARY this will go to somewhere within Boost.Geometry
|
||||||
|
namespace boost { namespace geometry
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace cs
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename Unit>
|
||||||
|
struct units_cartesian {};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace traits
|
||||||
|
{
|
||||||
|
template<typename U>
|
||||||
|
struct cs_tag<cs::units_cartesian<U> >
|
||||||
|
{
|
||||||
|
typedef cartesian_tag type;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace model
|
||||||
|
{
|
||||||
|
|
||||||
|
// Define a point type to interoperate with Boost.Units, having
|
||||||
|
// 1. a constructor taking quantities
|
||||||
|
// 2. defining a quantified coordinate system
|
||||||
|
// Note that all values are still stored in "normal" types as double
|
||||||
|
template <typename U, std::size_t D = 2, typename T = double, typename CS = cs::units_cartesian<U> >
|
||||||
|
class quantity_point : public model::point<T, D, CS>
|
||||||
|
{
|
||||||
|
typedef boost::units::quantity<U, T> qtype;
|
||||||
|
|
||||||
|
public :
|
||||||
|
|
||||||
|
// Templated constructor to allow constructing with other units then qtype,
|
||||||
|
// e.g. to convert from centimeters to meters
|
||||||
|
template <typename Q>
|
||||||
|
inline quantity_point(Q const& x, Q const& y)
|
||||||
|
: model::point<T, D, CS>(
|
||||||
|
qtype(x).value(),
|
||||||
|
qtype(y).value())
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Adapt quantity_point to the Point Concept
|
||||||
|
#ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
|
||||||
|
namespace traits
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename Units, std::size_t DimensionCount, typename CoordinateType, typename CoordinateSystem>
|
||||||
|
struct tag<model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem> >
|
||||||
|
{
|
||||||
|
typedef point_tag type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Units, std::size_t DimensionCount, typename CoordinateType, typename CoordinateSystem>
|
||||||
|
struct coordinate_type<model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem> >
|
||||||
|
{
|
||||||
|
typedef CoordinateType type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Units, std::size_t DimensionCount, typename CoordinateType, typename CoordinateSystem>
|
||||||
|
struct coordinate_system<model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem> >
|
||||||
|
{
|
||||||
|
typedef CoordinateSystem type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Units, std::size_t DimensionCount, typename CoordinateType, typename CoordinateSystem>
|
||||||
|
struct dimension<model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem> >
|
||||||
|
: boost::mpl::int_<DimensionCount>
|
||||||
|
{};
|
||||||
|
|
||||||
|
template<typename Units, std::size_t DimensionCount, typename CoordinateType, typename CoordinateSystem, std::size_t Dimension>
|
||||||
|
struct access<model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem>, Dimension >
|
||||||
|
{
|
||||||
|
static inline CoordinateType get(
|
||||||
|
model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem> const& p)
|
||||||
|
{
|
||||||
|
return p.template get<Dimension>();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void set(model::quantity_point<Units, DimensionCount, CoordinateType, CoordinateSystem>& p,
|
||||||
|
CoordinateType const& value)
|
||||||
|
{
|
||||||
|
p.template set<Dimension>(value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace traits
|
||||||
|
#endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// For extra support for functions as distance,area,get,set
|
||||||
|
namespace units
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
// Define an extra meta-function to get the units of a coordinate system
|
||||||
|
template <typename CS>
|
||||||
|
struct unit_dimension
|
||||||
|
{
|
||||||
|
// define it as dimensionless
|
||||||
|
// or MPL ASSERT
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
struct unit_dimension<cs::units_cartesian<U> >
|
||||||
|
{
|
||||||
|
typedef U type;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define an extra metafunction to define the quantity of a Geometry type
|
||||||
|
template <typename Geometry, typename CT = typename coordinate_type<Geometry>::type>
|
||||||
|
struct quantity
|
||||||
|
{
|
||||||
|
typedef boost::units::quantity
|
||||||
|
<
|
||||||
|
typename detail::unit_dimension
|
||||||
|
<
|
||||||
|
typename coordinate_system<Geometry>::type
|
||||||
|
>::type,
|
||||||
|
CT
|
||||||
|
> type;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Geometry1, typename Geometry2>
|
||||||
|
inline typename quantity<Geometry1, typename distance_result<Geometry1, Geometry2>::type>::type
|
||||||
|
distance(Geometry1 const& g1, Geometry2 const& g2)
|
||||||
|
{
|
||||||
|
typedef typename quantity<Geometry1, typename distance_result<Geometry1, Geometry2>::type>::type q;
|
||||||
|
return q::from_value(geometry::distance(g1, g2));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t Index, typename Point>
|
||||||
|
inline typename quantity<Point>::type get(Point const& p)
|
||||||
|
{
|
||||||
|
typedef typename quantity<Point>::type q;
|
||||||
|
return q::from_value(geometry::get<Index>(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}}
|
||||||
|
// END TEMPORARY
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
using namespace boost::geometry;
|
||||||
|
using namespace boost::units;
|
||||||
|
|
||||||
|
// 1: using it directly
|
||||||
|
{
|
||||||
|
typedef model::quantity_point<si::length, 2> point;
|
||||||
|
point p1(1 * si::meter, 2 * si::meter);
|
||||||
|
point p2(3 * si::meter, 4 * si::meter);
|
||||||
|
|
||||||
|
std::cout << get<0>(p2) << std::endl;
|
||||||
|
|
||||||
|
// This is a little inconvenient:
|
||||||
|
quantity<si::length> d = distance(p1, p2) * si::meter;
|
||||||
|
|
||||||
|
std::cout << d << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2: same but now using centimeters, and using boost::geometry::units::
|
||||||
|
{
|
||||||
|
typedef model::quantity_point<cgs::length, 2> point;
|
||||||
|
point p1(1 * si::meter, 2 * si::meter);
|
||||||
|
point p2(3 * si::meter, 4 * si::meter);
|
||||||
|
|
||||||
|
std::cout << boost::geometry::units::get<0>(p2) << std::endl;
|
||||||
|
quantity<cgs::length> d = boost::geometry::units::distance(p1, p2);
|
||||||
|
std::cout << d << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
171
example/08_units_example.vcproj
Normal file
171
example/08_units_example.vcproj
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
<?xml version="1.0" encoding="Windows-1252"?>
|
||||||
|
<VisualStudioProject
|
||||||
|
ProjectType="Visual C++"
|
||||||
|
Version="8.00"
|
||||||
|
Name="08_units_example"
|
||||||
|
ProjectGUID="{3D41FD4E-88B0-4A2A-9884-D434831A236C}"
|
||||||
|
RootNamespace="08_units_example"
|
||||||
|
Keyword="Win32Proj"
|
||||||
|
>
|
||||||
|
<Platforms>
|
||||||
|
<Platform
|
||||||
|
Name="Win32"
|
||||||
|
/>
|
||||||
|
</Platforms>
|
||||||
|
<ToolFiles>
|
||||||
|
</ToolFiles>
|
||||||
|
<Configurations>
|
||||||
|
<Configuration
|
||||||
|
Name="Debug|Win32"
|
||||||
|
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||||
|
IntermediateDirectory="$(ConfigurationName)\08_units_example"
|
||||||
|
ConfigurationType="1"
|
||||||
|
InheritedPropertySheets=".\boost.vsprops"
|
||||||
|
CharacterSet="1"
|
||||||
|
>
|
||||||
|
<Tool
|
||||||
|
Name="VCPreBuildEventTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCCustomBuildTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCXMLDataGeneratorTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCWebServiceProxyGeneratorTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCMIDLTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"
|
||||||
|
Optimization="0"
|
||||||
|
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
||||||
|
RuntimeLibrary="1"
|
||||||
|
DisableLanguageExtensions="false"
|
||||||
|
UsePrecompiledHeader="0"
|
||||||
|
DebugInformationFormat="1"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCManagedResourceCompilerTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCResourceCompilerTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCPreLinkEventTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCLinkerTool"
|
||||||
|
GenerateDebugInformation="true"
|
||||||
|
SubSystem="1"
|
||||||
|
TargetMachine="1"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCALinkTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCManifestTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCXDCMakeTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCBscMakeTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCFxCopTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCAppVerifierTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCWebDeploymentTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCPostBuildEventTool"
|
||||||
|
/>
|
||||||
|
</Configuration>
|
||||||
|
<Configuration
|
||||||
|
Name="Release|Win32"
|
||||||
|
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||||
|
IntermediateDirectory="$(ConfigurationName)\08_units_example"
|
||||||
|
ConfigurationType="1"
|
||||||
|
InheritedPropertySheets=".\boost.vsprops"
|
||||||
|
CharacterSet="1"
|
||||||
|
WholeProgramOptimization="1"
|
||||||
|
>
|
||||||
|
<Tool
|
||||||
|
Name="VCPreBuildEventTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCCustomBuildTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCXMLDataGeneratorTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCWebServiceProxyGeneratorTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCMIDLTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCCLCompilerTool"
|
||||||
|
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
|
||||||
|
UsePrecompiledHeader="0"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCManagedResourceCompilerTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCResourceCompilerTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCPreLinkEventTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCLinkerTool"
|
||||||
|
SubSystem="1"
|
||||||
|
OptimizeReferences="2"
|
||||||
|
EnableCOMDATFolding="2"
|
||||||
|
TargetMachine="1"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCALinkTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCManifestTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCXDCMakeTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCBscMakeTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCFxCopTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCAppVerifierTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCWebDeploymentTool"
|
||||||
|
/>
|
||||||
|
<Tool
|
||||||
|
Name="VCPostBuildEventTool"
|
||||||
|
/>
|
||||||
|
</Configuration>
|
||||||
|
</Configurations>
|
||||||
|
<References>
|
||||||
|
</References>
|
||||||
|
<Files>
|
||||||
|
<File
|
||||||
|
RelativePath=".\08_units_example.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
</Files>
|
||||||
|
<Globals>
|
||||||
|
</Globals>
|
||||||
|
</VisualStudioProject>
|
@ -20,6 +20,7 @@ exe 06_a_transformation_example : 06_a_transformation_example.cpp ;
|
|||||||
exe 06_b_transformation_example : 06_b_transformation_example.cpp ;
|
exe 06_b_transformation_example : 06_b_transformation_example.cpp ;
|
||||||
exe 07_a_graph_route_example : 07_a_graph_route_example.cpp ;
|
exe 07_a_graph_route_example : 07_a_graph_route_example.cpp ;
|
||||||
exe 07_b_graph_route_example : 07_b_graph_route_example.cpp ;
|
exe 07_b_graph_route_example : 07_b_graph_route_example.cpp ;
|
||||||
|
exe 08_units_example : 08_units_example.cpp ;
|
||||||
|
|
||||||
exe c01_custom_point_example : c01_custom_point_example.cpp ;
|
exe c01_custom_point_example : c01_custom_point_example.cpp ;
|
||||||
exe c02_custom_box_example : c02_custom_box_example.cpp ;
|
exe c02_custom_box_example : c02_custom_box_example.cpp ;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user