几何尺寸与公差论坛

 找回密码
 注册
查看: 112|回复: 0

when the considered feature is 2D round slot, please compute line profile

[复制链接]
发表于 2024-10-17 16:11:00 | 显示全部楼层 |阅读模式
#include <iostream>
#include <vector>
#include <cmath>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

// Define a 2D point structure
struct Point2D
{
    double x, y;
};

// Compute the Euclidean distance between two points
double computeDistance(const Point2D& p1, const Point2D& p2)
{
    return std::sqrt(std::pow(p1.x - p2.x, 2) + std::pow(p1.y - p2.y, 2));
}

// Function to calculate distance from point to rectangle boundary
double distanceToRectangle(double x, double y, double rectLeft, double rectRight, double rectTop, double rectBottom)
{
    if (x < rectLeft) return rectLeft - x;         // Left of rectangle
    if (x > rectRight) return x - rectRight;       // Right of rectangle
    if (y < rectBottom) return rectBottom - y;     // Below rectangle
    if (y > rectTop) return y - rectTop;           // Above rectangle
    return 0.0;                                    // Inside the rectangle
}

// Function to compute the line profile for a round slot
vector<double> computeLineProfile(double cx1, double cx2, double cy, double radius, double lineX, double yStart, double yEnd, int numSamples)
{
    vector<double> profile;
    double yStep = (yEnd - yStart) / numSamples;

    // Define the rectangle's top, bottom, left, and right boundaries
    double rectLeft = cx1;
    double rectRight = cx2;
    double rectTop = cy + radius;
    double rectBottom = cy - radius;

    for (int i = 0; i <= numSamples; ++i)
    {
        // Current y position on the line
        double y = yStart + i * yStep;

        // Point on the vertical line
        Point2D pointOnLine = {lineX, y};

        double distToBoundary;

        // If the point is aligned with the rectangle
        if (y >= rectBottom && y <= rectTop)
        {
            distToBoundary = distanceToRectangle(lineX, y, rectLeft, rectRight, rectTop, rectBottom);
        }
        else if (y < rectBottom)  // Below the rectangle, nearest to the bottom half-circle
        {
            Point2D circleCenter = {cx1, cy};  // Left half-circle center
            distToBoundary = std::abs(computeDistance(pointOnLine, circleCenter) - radius);
        }
        else  // Above the rectangle, nearest to the top half-circle
        {
            Point2D circleCenter = {cx2, cy};  // Right half-circle center
            distToBoundary = std::abs(computeDistance(pointOnLine, circleCenter) - radius);
        }

        // Store the result in the profile
        profile.push_back(distToBoundary);
    }

    return profile;
}

int main()
{
    // Define the round slot geometry
    double cx1 = 2.0;         // Center of the left half-circle
    double cx2 = 8.0;         // Center of the right half-circle
    double cy = 5.0;          // y-coordinate of the half-circles
    double radius = 1.0;      // Radius of the half-circles

    // Define the vertical line at x = 6.0, sample points along y-axis from y = 0 to y = 10
    double lineX = 6.0;
    double yStart = 0.0;
    double yEnd = 10.0;
    int numSamples = 100;

    // Compute the line profile for the round slot
    vector<double> profile = computeLineProfile(cx1, cx2, cy, radius, lineX, yStart, yEnd, numSamples);

    // Output the line profile
    cout << "Line profile distances to the slot boundary:" << endl;
    for (size_t i = 0; i < profile.size(); ++i)
    {
        cout << "y = " << yStart + i * ((yEnd - yStart) / numSamples) << ", distance to boundary = " << profile[i] << endl;
    }

    return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 13:33 , Processed in 0.033070 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表