<commit_before>// Copyright (C) 2016 <NAME>
// This file is part of the Ouzel engine.

#include "Component.h"
#include "Node.h"
#include "utils/Utils.h"
#include "math/MathUtils.h"

namespace ouzel
{
    namespace scene
    {
        Component::~Component()
        {
            if (node) node->removeComponent(this);
        }

        void Component::draw(const Matrix4&,
                             const graphics::Color&,
                             scene::Camera*)
        {
        }

        void Component::drawWireframe(const Matrix4&,
                                      const graphics::Color&,
                                      scene::Camera*)
        {
        }

        bool Component::pointOn(const Vector2& position) const
        {
            return boundingBox.containsPoint(position);
        }

        bool Component::shapeOverlaps(const std::vector<Vector2>& edges) const
        {
            uint8_t inCorners = 0;

            for (const Vector2& corner : edges)
            {
                if (corner.x >= boundingBox.min.x &&
                    corner.x <= boundingBox.max.x &&
                    corner.y >= boundingBox.min.y &&
                    corner.y <= boundingBox.max.y)
                {
                    return true;
                }

                if (corner.x < boundingBox.min.x && corner.y < boundingBox.min.y) inCorners |= 0x01;
                if (corner.x > boundingBox.max.x && corner.y < boundingBox.min.y) inCorners |= 0x02;
                if (corner.x > boundingBox.max.x && corner.y > boundingBox.max.y) inCorners |= 0x04;
                if (corner.x < boundingBox.min.x && corner.y > boundingBox.max.y) inCorners |= 0x08;
            }

            // bounding box is bigger than rectangle
            if (inCorners == 0x0F)
            {
                return true;
            }

            Vector2 boundingBoxCorners[4] = {
                Vector2(boundingBox.min),
                Vector2(boundingBox.max.x, boundingBox.min.y),
                Vector2(boundingBox.max),
                Vector2(boundingBox.min.x, boundingBox.max.y)
            };

            for (uint32_t current = 0; current < edges.size(); ++current)
            {
                uint32_t next = (current == (edges.size() -1)) ? 0 : current + 1;

                if (linesIntersect(Vector2(edges[current].x, edges[current].y), Vector2(edges[next].x, edges[next].y), boundingBoxCorners[0], boundingBoxCorners[1]) || // left
                    linesIntersect(Vector2(edges[current].x, edges[current].y), Vector2(edges[next].x, edges[next].y), boundingBoxCorners[1], boundingBoxCorners[2]) || // top
                    linesIntersect(Vector2(edges[current].x, edges[current].y), Vector2(edges[next].x, edges[next].y), boundingBoxCorners[2], boundingBoxCorners[3]) || // right
                    linesIntersect(Vector2(edges[current].x, edges[current].y), Vector2(edges[next].x, edges[next].y), boundingBoxCorners[3], boundingBoxCorners[0])) // bottom
                {
                    return true;
                }
            }

            return false;
        }
    } // namespace scene
} // namespace ouzel<commit_msg>Implement separating-axis search for shape overlap check<commit_after>// Copyright (C) 2016 <NAME>
// This file is part of the Ouzel engine.

#include "Component.h"
#include "Node.h"
#include "utils/Utils.h"
#include "math/MathUtils.h"

namespace ouzel
{
    namespace scene
    {
        Component::~Component()
        {
            if (node) node->removeComponent(this);
        }

        void Component::draw(const Matrix4&,
                             const graphics::Color&,
                             scene::Camera*)
        {
        }

        void Component::drawWireframe(const Matrix4&,
                                      const graphics::Color&,
                                      scene::Camera*)
        {
        }

        bool Component::pointOn(const Vector2& position) const
        {
            return boundingBox.containsPoint(position);
        }

        inline void gatherPolygonProjectionExtents(const std::vector<Vector2>& vertList,
                                                   const ouzel::Vector2& v,
                                                   float& outMin, float& outMax)
        {
            outMin = outMax = v.dot(vertList[0]);

            size_t vertListSize = vertList.size();
            for (size_t i = 1 ; i < vertListSize; ++i)
            {
                float d = v.dot(vertList[i]);
                if (d < outMin) outMin = d;
                else if (d > outMax) outMax = d;
            }
        }

        inline bool findSeparatingAxis(const std::vector<Vector2>& aVertList,
                                       const std::vector<Vector2>& bVertList)
        {
            ouzel::Vector2 v;

            size_t aVertListSize = aVertList.size();
            size_t prev = aVertListSize - 1;
            for (size_t cur = 0; cur < aVertListSize; ++cur)
            {
                ouzel::Vector2 edge = aVertList[cur] - aVertList[prev];

                v.x = edge.y;
                v.y = -edge.x;

                float aMin, aMax, bMin, bMax;
                gatherPolygonProjectionExtents(aVertList, v, aMin, aMax);
                gatherPolygonProjectionExtents(bVertList, v, bMin, bMax);

                if (aMax < bMin) return true;
                if (bMax < aMin) return true;

                prev = cur;
            }

            return false;
        }

        bool Component::shapeOverlaps(const std::vector<Vector2>& edges) const
        {
            std::vector<Vector2> boundingBoxEdges = {
                Vector2(boundingBox.min),
                Vector2(boundingBox.max.x, boundingBox.min.y),
                Vector2(boundingBox.max),
                Vector2(boundingBox.min.x, boundingBox.max.y)
            };

            if (findSeparatingAxis(boundingBoxEdges, edges))
                return false;

            if (findSeparatingAxis(edges, boundingBoxEdges))
                return false;

            return true;
        }
    } // namespace scene
} // namespace ouzel
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.012670565302144249, 'mean_word_length': 1.4826612903225806, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4707101', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1663, 'n_words': 351}