A Jalgorithm for Japplying Jeans to Jobjects
Dave Pagurek
March 2023

Abstract

2

Background

Gradient-based optimization has been a subject ofstudy in mathematics for a long time. The core idea
is that if one has an objective function one wishes tomaximize or minimize, one can use the gradient of
the output with respect to the function’s inputs todetermine how to adjust the inputs in order to movethe output closer to the desired outcome. There aremany algorithms, including Adam optimization [5],to update inputs in this manner using gradient information.
Differentiable rendering is a technique in the worldof computer graphics and computer vision that en1 Introduction
ables gradient-based optimization of functions involving rendering of images. If the rendering process isIn recent years, the internet has been ruminating over itself differentiable, then one could, for example, opthe question of how various objects would wear pants, timize the representation of a 3D scene such that
if they hypothetically wore pants. [2] It began in 2015 it matches a reference photo when rendered. [7, 8]
using a dog as the object in question, [3] with other The diffvg library [6] provides a differentiable vector
objects such as humans on their hands and knees [12] graphics renderer, allowing one to optimize 2D vector
shapes such as lines, curves, and polygons to produce
or pants themselves [4] being discussed later.
desired visuals.
Despite being in the public discourse for nearly adecade, no conclusions have been reached. The authors believe this is due to a lack of a principled ap- 3
Methodproach to the study of pants-wearing. This paper
aims to rectify the situation by proposing a formula 3.1 Algorithm
to score the quality of the application of pants to an
object, and writing software to optimally apply pants Our pants-fitting jalgorithm takes as input a vector
shape representing a silhouette on which one wants
under this system.
For years, society has been plagued by the lack of aconclusive answer to the question of how arbitrary
things would wear jeans, if they wore jeans. This
paper proposes a jalgorithm to determine the mathematically optimal configuration of pants on a silhouette, leveraging recent insights in the fields of computer vision and differentiable rendering.

2. No-stretch jeans: Pants, when applied to thewearer in rest position, should not be so deformed that they no longer resemble prototypicaljeans.
3. (Topologically) torn jean avoidance: Limbs
should only go through the open ends of pants,and similarly, closed ends of pants should notintersect limbs.
4. Dress code compliance: Pants should be on
the body of the wearer, not off of it.

Figure 1: Prototypical jeans: X, the initial pants
shape we apply to objects.

to apply pants. We rasterize this to a 256×256-pixel
image, concerning ourselves only with the alpha channel. We will refer to this one-channel silhouette image
as the 256×256 matrix S.
We then create a polygon representing the pants wewant to fit onto the silhouette, whose vertex positions
X we will optimize. We initialize its vertices to X, theshape of prototypical jeans, shown in Figure 1. We
then solve for the vertices X̂ that minimize pantsfitting loss, l(X), described in detail in Section 3.2,using Adam optimization [5] and gradients provided
via diffvg [6] vector rendering and Torch [11] for 100
iterations:X̂ = argminX l(X)(1)

From Rule 1, we get the following loss terms, where
xtopleft and xtopright correspond to the endpoints of
the waistband of prototypical jeans, and J(X) is the
256×256 matrix of the rendered alpha channel of thejeans vertices X. Lcenter incentivizes waistbands thatgo through the center of the silhouette, and Lcoverage
incentivizes jeans that cover half of the silhouette.
  2
xtopleft + xtopright0.5(2)−Lcenter =0.52
 2
Pmax(S−J(X)),0i,jij1
P
Lcoverage =  −2i,j Sij(3)
From Rule 2, we get the loss term Lrigid , which incentivizes the jean points X to be as close as possible
to an affine transformation of the prototypical jeanpoints X by finding the error of the least-squaresminimal transformation T that describes the relation
between X and X.
2Lrigid = ∥X − T X∥ ,p̂1 p̂2 p̂3
3.2 Loss Function
T = p̂4 p̂5 p̂6  ,0 0 1
Our loss function is motivated by insights from the authors’ multuple decades of pants-wearing experiX0,1
X0:1,1ence. We propose the following rules, which we then X1,1  0 0 will encode mathematically: p̂ = argminp  ...  −  X0,n  X0:1,n1. Duality of man: Pants should bisect the silX1,n0 0
houette of the wearer through its center, covering half its area.

(4)(5)
1 0 00 X0:1,1..
.
1 0 00 X0:1,n 201p01(6)From Rule 3, we get the terms Lopen and Lclosed ,terms incentivizing open and closed edges of the jeans
respectively to and to not intersect the silhouette,and Linter , incentivizing edges to not intersect eachother. We refer to the set of all edges as E(X), andthe of open and closed edges as Eo (X) and Ec (X).
The rendered alpha channel of an edge with stroke
thickness of 5 is referred to as R(e).
P− i,j R(Eo (X))ij Sij
P(7) Figure 2: Placement of jeans on a human via theLopen =algorithm, matching conventional wisdom of wherei,j R(Eo (X))ij
P
jeans belong on a human.
R(E(X))
Scij ij
i,jLclosed = P(8)i,j R(Ec (X))ij
PX
i,j R(e1 )ij R(e2 )ij
P
Linter =i,j min (R(e1 )ij + R(e2 )ij , 1){e1 ,e2 }:(E(X)
2 )(9)From Rule 4, we get Linside , discouraging the pantsfrom being off the silhouette:
Pi,j max (J(X)ij − Sij , 0)P
Linside =(10)i,j J(X)ij
We take a weighted sum over all the loss terms to Figure 3: Placement of jeans on the Vitruvian Man,
get the final loss function:
the body shape we all aspire to have.
Xl(X) =ki Li(11)
iFor our results, we used the following weights:
kcenter = 50,
kcoverage = 100,
krigid = 5,

silhouette of a human. Figure 2 shows that the algorithm placed the jeans on the legs of the human,
which the reader may recall is the standard configuration of human jeans.

While this is only a sample size of one, there isinfact only one species for whom jeans are origikclosed = 1,
nally designed, so we feel confident in our ability tokinter = 50,
extrapolate from this to other figures. However, askinside = 1
a gift to the reader, we test on one more humanshaped validation input: Leonardo da Vinci’s Vitruvian Man. Conceived as a depiction of ideal body
4 Results and Analysis
proportions, [1] the octopedal body plan represents
what we all aspire to look like. Figure 3 shows the4.1 Validation
algorithm confidently placing jeans on two of the four
To verify that the algorithm outlined in Section 3 op- legs, fashionably rolling up one leg in a manner remerates as expected for known cases, we ran it on the iniscent to one-strapping a backpack. [9]kopen = 1,

Figure 4: Placement of jeans on a dog via the algorithm. The jeans are placed on all legs, but in
an interesting deviation from the original hypothesis,
the jeans leg for the rear legs has ridden up to alignwith the derrière.

4.2

Novel Inputs

We next tested the algorithm on a dog in an attempt
to settle the original debate. The original image presented two hypotheses for dog jean placement: on
the rear legs only, subdividing the dog into two horizontal sections, or on all legs, subdividing the dog
into two vertical sections. [3] Figure 4 shows that thealgorithm places the jeans in a configuration much
more like the latter than the former, but with a twist:
rather than form-fitting the rear legs, the algorithm
chose to collapse the rear pants leg so that it ends atthe derrière of the dog.
In Figure 5, we present jeans applied to some morebody shapes. First, one best described as horseinspired (the literature shows that one likely cannot draw a good horse without reference materialpresent [10]) dons a single pant leg. Here, the algorithm demonstrates that it would rather shrivel up
a leg into nothing than stretch a waistband across
any part of a horse. Next, a three-legged alien wears
jeans on one and a half legs, with a small branch ofjean reaching up to its shoulder for support. This
demonstrates the algorithm’s ability to balance its
creative approach to jean design with functional concerns, like extra shoulder support. Finally, a spikyball dragon wears jeans on only one of its ball legs,
further demonstrating a preference for one-strapping.

Figure 5: Jean placements on alternative body
shapes. Note the creative one-strap overall look of
the tripod, and the bold asymmetric style of covering one ball of the ball dragon.

5Conclusion

We have presented a mathematically sound optimization approach to determining the optimal configuration of pants on any object, should it choose to wear
pants. Now that a definitive answer exists to thisquestion, we expect no further arguments to be madeon the subject on the internet. Since the algorithm’s
outputs are, by definition, optimal, one may choose
to take lessons from its output, and begin casuallyrolling up just one pant leg in one’s outfits.

References[1] Vitruvian man. Wikipedia. URL: https://en.
wikipedia.org/wiki/Vitruvian Man.

[7] Shichen Liu, Tianye Li, Weikai Chen, and Hao
Li. Soft rasterizer: A differentiable renderer forimage-based 3d reasoning. The IEEE International Conference on Computer Vision (ICCV),Oct 2019.
[8] Shichen Liu, Tianye Li, Weikai Chen, and Hao
Li. A general differentiable mesh renderer forimage-based 3d reasoning. IEEE Transactions
on Pattern Analysis and Machine Intelligence,2020.
[9] Phil Lord and Christopher Miller.
Street. 2012.

21 Jump[10] Dave Pagurek. Can you draw a horse without areference? Jul 2022. URL: https://davepagurek.
github.io/horse-drawings/.

[2] If a dog wore pants.
Know Your Meme, [11] Adam Paszke, Sam Gross, Soumith Chintala,Dec 2015. URL: https://knowyourmeme.com/
Gregory Chanan, Edward Yang, Zachary Dememes/if-a-dog-wore-pants.
Vito, Zeming Lin, Alban Desmaison, Luca
Antiga, and Adam Lerer. Automatic differen[3] If a dog wore pants would he wear themtiation in pytorch. In NIPS-W, 2017.
like this or like this?
Utopian Raspberry - Modern Oasis Machine, Dec 2015. [12] /u/flooid. If a man wore pants... Reddit, Dec
2015. URL: https://www.reddit.com/r/funny/
URL:
https://www.facebook.com/1ur.mom/comments/3yosk1/if a man wore pants/.
photos/a.535344933281912.1073741828.
535340893282316/604404026376002/?type=3&amp;theater.
[4] @bobby.
quick question.
Twitter, April
2017. URL: https://twitter.com/bobby/status/849491087005810688.
[5] Diederik P. Kingma and Jimmy Ba. Adam: A
method for stochastic optimization. In Yoshua
Bengio and Yann LeCun, editors, 3rd International Conference on Learning Representations,
ICLR 2015, San Diego, CA, USA, May 7-9,
2015, Conference Track Proceedings, 2015. URL:
http://arxiv.org/abs/1412.6980.
[6] Tzu-Mao Li, Michal Lukáč, Gharbi Michaël, andJonathan Ragan-Kelley. Differentiable vector
graphics rasterization for editing and learning.
ACM Trans. Graph. (Proc. SIGGRAPH Asia),
39(6):193:1–193:15, 2020.