Windows Phone开发(33):路径之其它Geometry

本文介绍如何使用XAML在Windows Phone应用中绘制四种基本图形:直线、矩形、椭圆及组合图形,并展示了每种图形的属性设置及实际效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一节中,我们把最复杂的PathGeometry给干了,生剩下几个家伙就好办事了。一起来见见他们的真面目吧。

一、LineGeometry

这个几何图形就很简单了,一条线段,两个点——StartPoint And EndPoint。

一起来看看下面的例子。

        <Path Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,5" EndPoint="200,320"/>
            </Path.Data>
        </Path>


运行之后你会看到以下情景:

二、RectangleGeometry

它呈现一人矩形的几何图形,Rect指示其中矩形的位置大小,在XAML中可以用4个数值表示,即X、Y、Width、Height;别外,RadiusX和RadiusY表示圆角在X轴和Y轴上的半径。看下面的例子。

        <Path Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="12,6,125,90" RadiusX="24" RadiusY="30"/>
            </Path.Data>
        </Path>


运行效果如下图所示。

三、EllipseGeometry

表示一个椭圆的几何图形,Center属性为椭圆的中心点的坐标,RadiusX和RadiusY分别为X轴方向上和Y轴方向上的半径长度。看例子。

        <Path Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="100,180" RadiusX="55" RadiusY="120"/>
            </Path.Data>
        </Path>


运行效果如下:

四、GeometryGroup

严格上说,它不属性一种几何图形,但它很有用,因为它可以同时包含N个几何图形,如下面例子所示。

        <Path Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <GeometryGroup>
                    <LineGeometry StartPoint="32,185" EndPoint="180,230"/>
                    <RectangleGeometry Rect="35,85,136,96" RadiusX="25" RadiusY="5"/>
                    <EllipseGeometry Center="112,130" RadiusX="45" RadiusY="36"/>
                </GeometryGroup>
            </Path.Data>
        </Path>


运行效是如下所示:

下面是本节示例的完整XAML代码。

<phone:PhoneApplicationPage 
    x:Class="Sample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <Style TargetType="Path">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="20"/>
            <Setter Property="Stroke" Value="Blue"/>
            <Setter Property="StrokeThickness" Value="8"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Path Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,5" EndPoint="200,320"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="12,6,125,90" RadiusX="24" RadiusY="30"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="100,180" RadiusX="55" RadiusY="120"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <GeometryGroup>
                    <LineGeometry StartPoint="32,185" EndPoint="180,230"/>
                    <RectangleGeometry Rect="35,85,136,96" RadiusX="25" RadiusY="5"/>
                    <EllipseGeometry Center="112,130" RadiusX="45" RadiusY="36"/>
                </GeometryGroup>
            </Path.Data>
        </Path>
    </Grid>
</phone:PhoneApplicationPage>


<think>我们正在清理一个存储路径点的向量:std::vector<geometry_msgs::PoseStamped>& plan 注意:这是一个引用,我们可能需要在函数中清空它,或者释放其内存。 但通常,在函数的开始,我们会清空传入的plan,以便填充新的路径。 然而,用户可能问的是如何彻底释放这个向量的内存(即不仅清空内容,还要释放占用的内存)。 因为std::vector的clear()方法只清空元素,不释放内存(capacity不变),所以我们需要用swap技巧来释放内存。</think>### 彻底清理 `std::vector<geometry_msgs::PoseStamped>& plan` 的完整方案 #### 1. 基本清理方法(清空内容) ```cpp // 清空向量内容但保留内存分配 plan.clear(); // 移除所有元素,size=0,但capacity不变 ``` #### 2. 彻底释放内存(推荐) ```cpp // 使用swap技巧完全释放内存 std::vector<geometry_msgs::PoseStamped>().swap(plan); // 验证结果 ROS_INFO("Plan capacity after swap: %zu", plan.capacity()); // 应输出0 ``` #### 3. 多场景清理策略 | **场景** | **清理方法** | **内存影响** | |----------|--------------|--------------| | 规划前准备 | `plan.clear()` | 快速复用内存 | | 错误处理时 | `plan = std::vector<geometry_msgs::PoseStamped>()` | 创建临时空向量 | | 节点关闭时 | `std::vector<geometry_msgs::PoseStamped>().swap(plan)` | 完全释放内存 | #### 4. 高级内存管理技巧 ```cpp // 方法1:使用shrink_to_fit()优化 plan.clear(); plan.shrink_to_fit(); // C++11+ 特性 // 方法2:移动语义转移所有权(C++11+) auto temp = std::move(plan); // plan变为空,内存由temp管理 // temp离开作用域时自动释放 // 方法3:自定义内存池 static boost::object_pool<std::vector<geometry_msgs::PoseStamped>> planPool; auto& plan = *planPool.malloc(); // 从内存池分配 // 使用后... planPool.free(&plan); // 返回内存池 ``` #### 5. ROS特定场景最佳实践 ```cpp bool GlobalPlanner::makePlan(...) { // 规划前:确保容器为空 if(!plan.empty()) { ROS_WARN("Plan vector not empty! Forcing cleanup"); std::vector<geometry_msgs::PoseStamped>().swap(plan); } // ...路径规划逻辑... // 错误处理时:彻底清理 if(plan_failed) { std::vector<geometry_msgs::PoseStamped>().swap(plan); return false; } // 正常返回:保留内存供外部使用 return true; } // 在节点关闭时 GlobalPlanner::~GlobalPlanner() { // 确保所有引用plan的变量都释放 std::vector<geometry_msgs::PoseStamped> empty; plan.swap(empty); // 安全释放 } ``` > **关键提示**:在ROS中,`nav_msgs::Path`消息发布后会自动管理内存,无需手动清理其内部的`poses`向量。 ### 内存清理验证方法 ```cpp #include <malloc.h> void checkVectorMemory(const std::vector<geometry_msgs::PoseStamped>& v) { ROS_INFO("Vector status: size=%zu, capacity=%zu", v.size(), v.capacity()); malloc_stats(); // 打印内存分配统计(Linux) } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值