All Projects → paulz → Perspectivetransform

paulz / Perspectivetransform

Licence: mit
Calculate CATransform3D between two Perspectives

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Perspectivetransform

PolyDraw
✳️ PTSource PolyDraw is a free 3D polygonal modeller for Windows x86 and x64, for creating or modifying 3D objects using a mesh of 3D points and parametric NURBS Curves .Exports and imports to over 40 formats including WebVR and 3D Printing.
Stars: ✭ 17 (-84.96%)
Mutual labels:  polygon, 3d-graphics
Mathsharp
A vector and matrix library written in C# using hardware intrinsics
Stars: ✭ 616 (+445.13%)
Mutual labels:  matrix, 3d-graphics
Sharpmath
A small .NET math library.
Stars: ✭ 36 (-68.14%)
Mutual labels:  matrix, polygon
Progressmeter
Measuring the progress with annotations 🔱
Stars: ✭ 107 (-5.31%)
Mutual labels:  xcode
Appicon
AppIcon generates *.appiconset contains each resolution image for iOS
Stars: ✭ 1,454 (+1186.73%)
Mutual labels:  xcode
Matrix Puppet Imessage
A two-way puppeted Matrix bridge for Apple iMessage / Messages
Stars: ✭ 109 (-3.54%)
Mutual labels:  matrix
Agsimpleimageeditorview
Yet Another Image Editor for iOS.
Stars: ✭ 112 (-0.88%)
Mutual labels:  xcode
Buckets Swift
Swift Collection Data Structures Library
Stars: ✭ 106 (-6.19%)
Mutual labels:  matrix
Device
Light weight tool for detecting the current device and screen size written in swift.
Stars: ✭ 1,503 (+1230.09%)
Mutual labels:  xcode
Materialactivityindicator
Material Activity Indicator
Stars: ✭ 109 (-3.54%)
Mutual labels:  xcode
The Matrix Effect
The incredible effect of rain of letters in the style of the Matrix trilogy.
Stars: ✭ 109 (-3.54%)
Mutual labels:  matrix
Swifterswift
A handy collection of more than 500 native Swift extensions to boost your productivity.
Stars: ✭ 10,706 (+9374.34%)
Mutual labels:  xcode
Densepoint
DensePoint: Learning Densely Contextual Representation for Efficient Point Cloud Processing (ICCV 2019)
Stars: ✭ 110 (-2.65%)
Mutual labels:  3d-graphics
Easing
Easing functions in Swift 5
Stars: ✭ 107 (-5.31%)
Mutual labels:  xcode
Shari
Shari is the alternative to the library of UIPickerView(drum roll) in Swift. You can select a item using UITableView.
Stars: ✭ 111 (-1.77%)
Mutual labels:  xcode
Nextgrowingtextview
📝 The next in the generations of 'growing textviews' optimized for iOS 8 and above.
Stars: ✭ 1,540 (+1262.83%)
Mutual labels:  xcode
Hmap
hmap is a command line tool written in Swift to work with Clang header maps produced by Xcode.
Stars: ✭ 110 (-2.65%)
Mutual labels:  xcode
React Native Settings
Allows access to various Android and iOS device settings using React Native
Stars: ✭ 108 (-4.42%)
Mutual labels:  xcode
Opencvjs
Complete opencvjs (With the lastest OpenCV 4.0.0+)
Stars: ✭ 108 (-4.42%)
Mutual labels:  matrix
Moderncppci
This is an example of doing a Modern C++ project with CI
Stars: ✭ 109 (-3.54%)
Mutual labels:  xcode

Perspective Transform

macOS iOS codecov Version License Platform Carthage compatible Swift

PerspectiveTransform caclulates homogeneous transformation matrix for a view 3D projection in 2D. It can be used to place views within given image visual perspective using Core Animation CATransform3D and CALayer.tranform. Projection is caclulated between source and destination perspectives that are defined by 4 corners, assuming they form a Quadrilateral.

CATransform3D

To place an overlay image on top of a container image with matching persperctive we can use Core Animation transform matrix. CATransform3D is a tranformation matrix that is used to rotate, scale, translate, skew, and project the layer content. It can also be used to describe a perspective projectection of 2D shape in 3D space.

Container Overlay Combination
Container Overlay Combined image

Core Animation allow applying CATransform3D to CALayer via transform property:

let layer = UIView().layer
layer.transform = CATransform3D(m11: sX,  m12: r12, m13: r13, m14: 0,
                                m21: r21, m22: sY,  m23: r23, m24: 0,
                                m31: r31, m32: r32, m33: 0,   m34: 0,
                                m41: tX,  m42: tY,  m43: 0,   m44: 1)

In detail CATransform3D is a 4 x 4 matrix which takes 16 parameters to build.

Translation and scale are represented by their axis components: (tX, tY) and (sX, sY) within the matrix. While 3D rotation is represented by multiple values: r12, r21, r13, r31, r32, r23.

Perspective Transform based on 4 corners

We can easily see 4 points with container image where the corners of the overlay image should be. In general it is a 4 point polygon. Using an SVG editor we can draw that polygon using container image as a background. Here is preview of the SVG file desribing placement of iPad screen corners

SVG polygon

Click image to see original SVG file defining polygon with points. From those 4 points we can calculate nessesary CATransform3D matrix using this PerspectiveTransform library.

Visual Example

We can see how 4 points polygon fits on the background image:

SVG polygon

SVG Points

We can even take coordinates of those 4 points from SVG file:

<polygon points="377.282671 41.4352201 459.781253 251.836131 193.321418 330.023027 108.315837 80.1687782 "></polygon>

Those are 4 pairs of X and Y coordinates:

377.282671 41.4352201
459.781253 251.836131
193.321418 330.023027
108.315837 80.1687782

Swift Code Example

Here is complete example of placing overlay view using those coordinates:

import PerspectiveTransform

// note order: top left, top right, bottom left, bottom right
let destination = Perspective(
    CGPoint(x: 108.315837, y: 80.1687782),
    CGPoint(x: 377.282671, y: 41.4352201),
    CGPoint(x: 193.321418, y: 330.023027),
    CGPoint(x: 459.781253, y: 251.836131)
)

// Starting perspective is the current overlay frame
let start = Perspective(overlayView.frame)

// Caclulate CATransform3D from start to destination
overlayView.layer.transform = start.projectiveTransform(destination: destination)

CALayer transform

Since CALayer transform is animatable property we can easily define smooth transition:

SVG polygon

Example Project

See Example iOS project illustating animation and interactive tranform within view controllers. To build Example project run pod install within Example folder.

Example project also includes Swift Playground with couple of live examples.

Installation

PerspectiveTransform is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'PerspectiveTransform'

Useful Links

Author

Paul Zabelin, http://github.com/paulz

License

PerspectiveTransform is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].