zoukankan      html  css  js  c++  java
  • [算法][包围盒]AABB简单类

    头文件:

    #pragma once
    #include <iostream>
    //一个假的点类型
    struct Vector3
    {
        float x;
        float y;
        float z;
    };
    
     
    class AABB
    {
    public:
        AABB();
        AABB(const AABB &aabb);
        ~AABB();
        void add(const Vector3 &v);
        void clear();
        void makeAABB(Vector3 V[], int n);
     
        Vector3 min, max, center;
    };
     
    void aabbPrint(AABB &ab);

    cpp文件

    #include "AABB.h"
    
    
    AABB::AABB()
    {
    }
     
    AABB::AABB(const AABB &aabb)
    {
        min = aabb.min;
        max = aabb.max;
        center = aabb.center;
    }
     
    void AABB::clear()
    {
        min.x = min.y = min.z = FLT_MAX;
        //careful: This is not FLT_MIN, 'cause FLT_MIN is a little larger than 0,
        //hence not a minus value.
        max.x = max.y = max.z = -FLT_MAX;
    }
     
    void AABB::add(const Vector3 &v)
    {
        if (v.x < min.x) min.x = v.x;
        if (v.y < min.y) min.y = v.y;
        if (v.z < min.z) min.z = v.z;
        if (v.x > max.x) max.x = v.x;
        if (v.y > max.y) max.y = v.y;
        if (v.z > max.z) max.z = v.z;
    }
     
    //make AABB out of Vector3 points
    void AABB::makeAABB(Vector3 V[], int n)
    {
        if (!V) return;
     
        for (int i = 0; i < n; i++)
            add(V[i]);
     
        center.x = (min.x + max.x) * 0.5f;
        center.y = (min.y + max.y) * 0.5f;
        center.z = (min.z + max.z) * 0.5f;
    }
     
    AABB::~AABB()
    {
    }
     
    void aabbPrint(AABB &ab)
    {
        std::cout<<"AABB min is: ";
        ab.min.x;
        ab.min.y;
        ab.min.z;
        std::cout<<"AABB max is: ";
        ab.max.x;
        ab.max.y;
        ab.max.z;
        std::cout<<std::endl; 
    }

    打印什么的自己看着办,没时间搞

  • 相关阅读:
    Java 密码扩展无限制权限策略文件
    深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
    git 常用功能 _fei
    ActiveMQ 使用
    【dp】导弹拦截
    【dp】求最长上升子序列
    【贪心】1225 金银岛
    最大子矩阵
    归并排序求逆序对
    服务器安全部署文档
  • 原文地址:https://www.cnblogs.com/lyggqm/p/5386340.html
Copyright © 2011-2022 走看看