Skip to content
Calculation.py 15.9 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
####################################################################################################
#
# PyValentina - A Python implementation of Valentina Pattern Drafting Software
# Copyright (C) 2017 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################

####################################################################################################

import logging

from .Calculator import Expression
from Valentina.Geometry.Line2D import Line2D
from Valentina.Geometry.Vector2D import Vector2D

####################################################################################################

_module_logger = logging.getLogger(__name__)

####################################################################################################

class Calculation:

    _logger = _module_logger.getChild('Calculation')

    ##############################################

    def __init__(self, pattern, id):

        self._pattern = pattern
        self._id = id

    ##############################################

    @property
    def id(self):
        return self._id

    @property
    def pattern(self):
        return self._pattern

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._id}'.format(self)

    ##############################################

    def eval(self):

        self._logger.info('Eval {}'.format(self))
        self.eval_internal()

    ##############################################

    def eval_internal(self):

        pass

####################################################################################################

class LinePropertiesMixin:

    ##############################################

    def __init__(self, line_style, line_color):

        self._line_color = line_color
        self._line_style = line_style

    ##############################################

    @property
    def line_color(self):
        return self._line_color

    @line_color.setter
    def line_color(self, value):
        self._line_color = value

    @property
    def line_style(self):
        return self._line_style

    @line_style.setter
    def line_style(self, value):
        self._line_style = value

####################################################################################################

class FirstSecondPointMixin:

    ##############################################

    def __init__(self, first_point, second_point):

        self._first_point = self._pattern.get_calculation(first_point)
        self._second_point = self._pattern.get_calculation(second_point)

    ##############################################

    @property
    def first_point(self):
        return self._first_point

    @property
    def second_point(self):
        return self._second_point

####################################################################################################

class BasePointMixin:

    ##############################################

    def __init__(self, base_point):

        self._base_point = self._pattern.get_calculation(base_point)

    ##############################################

    @property
    def base_point(self):
        return self._base_point

####################################################################################################

class LengthMixin:

    ##############################################

    def __init__(self, length):

        self._length = Expression(length, self._pattern.calculator)

    ##############################################

    @property
    def length(self):
        return self._length

####################################################################################################

class AngleMixin:

    ##############################################

    def __init__(self, angle):

        self._angle = Expression(angle, self._pattern.calculator)

    ##############################################

    @property
    def angle(self):
        return self._angle

####################################################################################################

class LengthAngleMixin(LengthMixin, AngleMixin):

    ##############################################

    def __init__(self, length, angle):

        LengthMixin.__init__(self, length)
        AngleMixin.__init__(self, angle)

####################################################################################################

class Point(Calculation):

    ##############################################

    def __init__(self, pattern, id, name, label_offset):

        Calculation.__init__(self, pattern, id)
        self._name = name
        self._label_offset = label_offset

        self._vector = None

    ##############################################

    @property
    def name(self):
        return self._name

    @property
    def label_offset(self):
        return self._label_offset

    @property
    def vector(self):
        return self._vector

    ##############################################

    def _post_eval_internal(self):

        self._logger.info('{0._name} {0._vector}'.format(self))

####################################################################################################

class SinglePoint(Point):

    ##############################################

    def __init__(self, pattern, id, name,
                 x, y,
                 label_offset,
                 ):

        Point.__init__(self, pattern, id, name, label_offset)
        self._x = Expression(x, pattern.calculator)
        self._y = Expression(y, pattern.calculator)

    ##############################################

    @property
    def x(self):
        return self._x

    @property
    def y(self):
        return self._y

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._name} = ({0._x}, {0._y})'.format(self)

    ##############################################

    def eval_internal(self):

        self._vector = Vector2D(self._x.value, self._y.value)
        self._post_eval_internal()

####################################################################################################

class AlongLinePoint(Point, LinePropertiesMixin, FirstSecondPointMixin, LengthMixin):

    ##############################################

    def __init__(self, pattern, id, name,
                 first_point, second_point, length,
                 label_offset,
                 line_style=None, line_color=None,
                 ):

        Point.__init__(self, pattern, id, name, label_offset)
        LinePropertiesMixin.__init__(self, line_style, line_color)
        FirstSecondPointMixin.__init__(self, first_point, second_point)
        LengthMixin.__init__(self, length)

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._name} = ({0._first_point.name}, {0._second_point.name}, {0._length})'.format(self)

    ##############################################

    def eval_internal(self):

        vector = self._second_point.vector - self._first_point.vector
        self._pattern.calculator.set_current_segment(vector)
        self._vector = self._first_point.vector + vector.to_normalised()*self._length.value
        self._pattern.calculator.unset_current_segment()
        self._post_eval_internal()

####################################################################################################

class EndLinePoint(Point, LinePropertiesMixin, BasePointMixin, LengthAngleMixin):

    ##############################################

    def __init__(self, pattern, id, name,
                 base_point, angle, length,
                 label_offset,
                 line_style=None, line_color=None,
                 ):

        Point.__init__(self, pattern, id, name, label_offset)
        LinePropertiesMixin.__init__(self, line_style, line_color)
        BasePointMixin.__init__(self, base_point)
        LengthAngleMixin.__init__(self, length, angle)

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._name} = ({0._base_point.name}, {0._angle}, {0._length})'.format(self)

    ##############################################

    def eval_internal(self):

        self._vector = self._base_point._vector + Vector2D.from_angle(self._angle.value)*self._length.value
        self._post_eval_internal()

####################################################################################################

class LineIntersectPoint(Point, LinePropertiesMixin):

    ##############################################

    def __init__(self, pattern, id, name,
                 point1_line1, point2_line1, point1_line2, point2_line2,
                 label_offset,
                 line_style=None, line_color=None,
                 ):

        Point.__init__(self, pattern, id, name, label_offset)
        LinePropertiesMixin.__init__(self, line_style, line_color)
        self._point1_line1 = pattern.get_calculation(point1_line1)
        self._point2_line1 = pattern.get_calculation(point2_line1)
        self._point1_line2 = pattern.get_calculation(point1_line2)
        self._point2_line2 = pattern.get_calculation(point2_line2)

    ##############################################

    @property
    def point1_line1(self):
        return self._point1_line1

    @property
    def point2_line1(self):
        return self._point2_line1

    @property
    def point1_line2(self):
        return self._point1_line2

    @property
    def point2_line2(self):
        return self._point2_line2

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._name} = ({0._point1_line1.name}, {0._point2_line1.name}, {0._point1_line2.name}, {0._point2_line2.name})'.format(self)

    ##############################################

    def eval_internal(self):

        line1 = Line2D(self._point1_line1.vector, self._point2_line1.vector)
        line2 = Line2D(self._point1_line2.vector, self._point2_line2.vector)
        self._vector = line1.intersection(line2)
        self._post_eval_internal()

####################################################################################################

class NormalPoint(Point, LinePropertiesMixin, FirstSecondPointMixin, LengthAngleMixin):

    ##############################################

    def __init__(self, pattern, id, name,
                 first_point, second_point, angle, length,
                 label_offset,
                 line_style=None, line_color=None,
                 ):

        Point.__init__(self, pattern, id, name, label_offset)
        LinePropertiesMixin.__init__(self, line_style, line_color)
        FirstSecondPointMixin.__init__(self, first_point, second_point)
        LengthAngleMixin.__init__(self, length, angle)

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._name} = ({0._first_point.name}, {0._second_point.name}, {0._angle}, {0._length})'.format(self)

    ##############################################

    def eval_internal(self):

        vector = self._second_point.vector - self._first_point.vector
        self._pattern.calculator.set_current_segment(vector)
        direction = vector.to_normalised()
        direction = direction.rotate_counter_clockwise_90()
        angle = self._angle.value
        if angle:
            direction = direction.rotate_counter_clockwise(angle)
        self._vector = self._first_point.vector + direction*self._length.value
        self._pattern.calculator.unset_current_segment()
        self._post_eval_internal()

####################################################################################################

class PointOfIntersection(Point, FirstSecondPointMixin):

    ##############################################

    def __init__(self, pattern, id, name,
                 first_point, second_point,
                 label_offset,
                 ):

        Point.__init__(self, pattern, id, name, label_offset)
        FirstSecondPointMixin.__init__(self, first_point, second_point)

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' {0._name} = ({0._first_point.name}, {0._second_point.name})'.format(self)

    ##############################################

    def eval_internal(self):

        self._vector = Vector2D(self._first_point.vector.x, self._second_point.vector.y)
        self._post_eval_internal()

####################################################################################################

class Line(Calculation, LinePropertiesMixin, FirstSecondPointMixin):

    ##############################################

    def __init__(self, pattern, id,
                 first_point, second_point,
                 line_style=None, line_color=None,
                 ):

        Calculation.__init__(self, pattern, id)
        LinePropertiesMixin.__init__(self, line_style, line_color)
        FirstSecondPointMixin.__init__(self, first_point, second_point)

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' ({0._first_point.name}, {0._second_point.name})'.format(self)

    ##############################################

    def eval_internal(self):

        pass

####################################################################################################

class SimpleInteractiveSpline(Calculation, LinePropertiesMixin, FirstSecondPointMixin):

    ##############################################

    def __init__(self, pattern, id,
                 first_point, second_point,
                 angle1, length1,
                 angle2, length2,
                 line_style=None, line_color=None,
                 ):

        Calculation.__init__(self, pattern, id)
        LinePropertiesMixin.__init__(self, line_style, line_color)
        FirstSecondPointMixin.__init__(self, first_point, second_point)
        self._angle1 = Expression(angle1, pattern.calculator)
        self._length1 = Expression(length1, pattern.calculator)
        self._angle2 = Expression(angle2, pattern.calculator)
        self._length2 = Expression(length2, pattern.calculator)

        self._control_point1 = None # Fixme: not yet computed
        self._control_point2 = None

    ##############################################

    @property
    def angle1(self):
        return self._angle1

    @property
    def length1(self):
        return self._length1

    @property
    def angle2(self):
        return self._angle2

    @property
    def length2(self):
        return self._length2

    @property
    def control_point1(self):
        return self._control_point1

    @property
    def control_point2(self):
        return self._control_point2

    ##############################################

    def __repr__(self):

        return self.__class__.__name__ + ' ({0._first_point.name}, {0._second_point.name}, {0._angle1}, {0._length1}, {0._angle2}, {0._length2})'.format(self)

    ##############################################

    def eval_internal(self):

        self._control_point1 = self.first_point.vector + Vector2D.from_angle(self._angle1.value)*self._length1.value
        self._control_point2 = self.second_point.vector + Vector2D.from_angle(self._angle2.value)*self._length2.value
        self._logger.info("Control points : {} {}".format(self._control_point1, self._control_point2))