I'm creating a mobius strip in Java3D. I've got a seam and I can't seem to get rid of it! I'm assuming it's got to do with normals and the fact that the difference in angle between the conjoined edges is technically 180. Can anyone help me remove this seam?
Here's my code:
public class MobiusStrip extends Applet {
public static void main(String[] args){
new MainFrame(new MobiusStrip(), 800, 600);
}
#Override
public void init(){
GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(gc);
this.setLayout(new BorderLayout());
this.add(canvas, BorderLayout.CENTER);
SimpleUniverse su = new SimpleUniverse(canvas);
su.getViewingPlatform().setNominalViewingTransform();
BranchGroup bg = createSceneGraph();
bg.compile();
su.addBranchGraph(bg);
}
private BranchGroup createSceneGraph(){
BranchGroup root = new BranchGroup();
Shape3D shape = new Shape3D();
shape.setGeometry(mobius().getIndexedGeometryArray());
//Scaling transform
Transform3D tr = new Transform3D();
tr.setScale(0.5);
//Spin transform group
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(spin);
//Set appearance
Appearance ap = new Appearance();
PointAttributes pa = new PointAttributes(10, true);
ap.setPointAttributes(pa);
ap.setPolygonAttributes(new PolygonAttributes
(PolygonAttributes.POLYGON_FILL,
PolygonAttributes.CULL_NONE, 0));
//Set materials
Material mat = new Material();
mat.setLightingEnable(true);
mat.setShininess(30);
ap.setMaterial(mat);
//Overarching Transform group
TransformGroup tg = new TransformGroup(tr);
tg.addChild(shape);
spin.addChild(tg);
shape.setAppearance(ap);
//Set rotation
Alpha alpha = new Alpha(-1, 6000);
RotationInterpolator rotate = new RotationInterpolator(alpha, spin);
BoundingSphere bounds = new BoundingSphere();
rotate.setSchedulingBounds(bounds);
spin.addChild(rotate);
//Set background
Background background = new Background(1.0f, 1.0f, 1.0f);
background.setApplicationBounds(bounds);
root.addChild(background);
//Set lighting
AmbientLight light = new AmbientLight(true, new Color3f(Color.BLACK));
light.setInfluencingBounds(bounds);
root.addChild(light);
PointLight ptlight = new PointLight(new Color3f(Color.white),
new Point3f(0.5f,0.5f,1f),
new Point3f(1f,0.2f,0f));
ptlight.setInfluencingBounds(bounds);
root.addChild(ptlight);
return root;
}//Close branchgroup method
//Create the Mobius shape
private GeometryInfo mobius()
{
int m = 100; //number of row points
int n = 100; //number of col points
int p = 4*((m-1)*(n-1)); //faces * points per face
IndexedQuadArray iqa = new IndexedQuadArray(m*n,
GeometryArray.COORDINATES, p);
Point3d[] vertices = new Point3d[m*n];
int index = 0;
//Create vertices
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
double u = i * (2*(Math.PI))/(m - 1);
double v = -0.3 + (j * (0.6/(n-1)));
double x=(1+v*Math.cos(u/2))*Math.cos(u);
double y=(1+v*Math.cos(u/2))*Math.sin(u);
double z=v*Math.sin(u/2);
vertices[index]=new Point3d(x,y,z);
index++;
}//close nested for loop
}//close for loop
iqa.setCoordinates(0, vertices);
index = 0;
//set index for coordinates
for(int i = 0; i < m-1; i++){
for(int j = 0; j < n-1; j++){
iqa.setCoordinateIndex(index, i*m+j);
index++;
iqa.setCoordinateIndex(index, i*m+j+1);
index++;
iqa.setCoordinateIndex(index, (i+1)*m+j+1);
index++;
iqa.setCoordinateIndex(index, (i+1)*m+j);
index++;
}//close nested for loop
}//close for loop
//create geometry info and generate normals for shape
GeometryInfo gi = new GeometryInfo(iqa);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
return gi;
}
}
See this question for more explanation. You'll need two changes:
ap.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_BACK, 0));
double u = i * (4 * (Math.PI)) / (m - 1);
Related
Annotation(PdfName.STAMP) missed after flattened by itextsharp5.5.13.1.
I have two pdfs. One does work, the other does not work.
Any ideas will be appreciated.
The code is following
string outFile = inputFile + "_f.pdf";
using (PdfReader pdfReader = new PdfReader(inputFileName))
{
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outFile, FileMode.Create, FileAccess.Write, FileShare.None))
//pdfStamper.FormFlattening = true;
//pdfStamper.FreeTextFlattening = true;
pdfStamper.AnnotationFlattening = true;
//pdfStamper.AcroFields.GenerateAppearances = true;
}
The cause is a bug in iTextSharp, annotation flattening does not correctly calculate the position for the flattened annotation if its bounding box does not use the origin as lower left corner.
If you look at the code of PdfStamperImp.FlattenAnnotations(bool), you'll quickly realize that the calculations in the if (app != null) block only make sense if the bounding box is located at the origin or no scaling is necessary for fitting the appearance bounding box into the annotation rectangle.
(As often the lower left corner of the bounding box is the origin, this does not pop up often.)
Thus, for flattening such annotations you have to use a different method for flattening, e.g. like this:
using (PdfReader pdfReader = new PdfReader(inputFileName))
{
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outFile, FileMode.Create, FileAccess.Write, FileShare.None));
ImprovedAnnotationFlattening(pdfStamper);
pdfStamper.Close();
}
with these helper methods:
void ImprovedAnnotationFlattening(PdfStamper pdfStamper)
{
double[] DEFAULT_MATRIX = { 1, 0, 0, 1, 0, 0 };
PdfReader reader = pdfStamper.Reader;
for (int page = 1; page <= reader.NumberOfPages; ++page)
{
PdfDictionary pageDic = reader.GetPageN(page);
PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS);
if (annots == null)
continue;
for (int idx = 0; idx < annots.Size; ++idx)
{
PdfObject annoto = annots.GetDirectObject(idx);
if (!(annoto is PdfDictionary))
continue;
PdfDictionary annDic = (PdfDictionary)annoto;
PdfNumber ff = annDic.GetAsNumber(PdfName.F);
int flags = ff != null ? ff.IntValue : 0;
if ((flags & PdfFormField.FLAGS_PRINT) == 0 || (flags & PdfFormField.FLAGS_HIDDEN) != 0)
continue;
PdfObject obj1 = annDic.Get(PdfName.AP);
if (obj1 == null)
continue;
PdfDictionary appDic = obj1 is PdfIndirectReference
? (PdfDictionary)PdfReader.GetPdfObject(obj1)
: (PdfDictionary)obj1;
PdfObject obj = appDic.Get(PdfName.N);
PdfStream objDict = appDic.GetAsStream(PdfName.N);
if (objDict != null)
{
Rectangle rect = PdfReader.GetNormalizedRectangle(annDic.GetAsArray(PdfName.RECT));
Rectangle bbox = PdfReader.GetNormalizedRectangle(objDict.GetAsArray(PdfName.BBOX));
PdfContentByte cb = pdfStamper.GetOverContent(page);
cb.SetLiteral("Q ");
PdfArray matrixArray = objDict.GetAsArray(PdfName.MATRIX);
double[] matrix = matrixArray != null ? matrixArray.AsDoubleArray() : DEFAULT_MATRIX;
AffineTransform transform = new AffineTransform(matrix);
double[] bboxCorners = { bbox.Left, bbox.Bottom, bbox.Right, bbox.Bottom, bbox.Right, bbox.Top, bbox.Left, bbox.Top };
transform.Transform(bboxCorners, 0, bboxCorners, 0, 4);
double minX = Min(bboxCorners, 0, 2);
double maxX = Max(bboxCorners, 0, 2);
double minY = Min(bboxCorners, 1, 2);
double maxY = Max(bboxCorners, 1, 2);
transform.preConcatenate(AffineTransform.GetTranslateInstance(-minX, -minY));
transform.preConcatenate(AffineTransform.GetScaleInstance(rect.Width/(maxX-minX), rect.Height/(maxY-minY)));
transform.preConcatenate(AffineTransform.GetTranslateInstance(rect.Left, rect.Bottom));
transform.GetMatrix(matrix);
cb.AddFormXObj(objDict, GenerateName(), matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
cb.SetLiteral("q ");
annots.Remove(idx);
--idx;
}
}
}
}
double Min(double[] array, int start, int step)
{
double result = array[start];
for (int i = start + step; i < array.Length; i+=step)
{
result = Math.Min(result, array[i]);
}
return result;
}
double Max(double[] array, int start, int step)
{
double result = array[start];
for (int i = start + step; i < array.Length; i += step)
{
result = Math.Max(result, array[i]);
}
return result;
}
PdfName GenerateName()
{
PdfName name = new PdfName("XXX" + formXObjectsCounter);
++formXObjectsCounter;
return name;
}
int formXObjectsCounter = 4711;
Beware: I just wrote these methods (copying as much as possible from the original flattening code) and only tested with your example files. Some border conditions might still have to be considered for general use. In particular I did not do all relevant null or 0 tests. Also I did not attempt to support proper tagging.
I would like to add and remove a watermark to a PDF using iText 7. I was able to add the watermark, but unable to remove it again. I could only find relevant code/examples related to iText 5. Any pointers appreciated, thanks.
This is how I added the Watermark (using Layers):
pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
var numberOfPages = pdfDoc.GetNumberOfPages();
PageSize ps = pdfDoc.GetDefaultPageSize();
for (var i = 1; i <= numberOfPages; i++)
{
PdfPage page = pdfDoc.GetPage(i);
PdfLayer layer = new PdfLayer("watermark", pdfDoc);
var canvas = new PdfCanvas(page);
var pageSize = page.GetPageSize();
var paragraph = new Paragraph(message.WatermarkText).SetFontSize(60);
paragraph.SetFontColor(Color.BLACK, 0.2f);
Canvas canvasModel;
canvas.BeginLayer(layer);
canvasModel = new Canvas(canvas, pdfDoc, ps);
canvasModel.ShowTextAligned(paragraph, pageSize.GetWidth() / 2, pageSize.GetHeight() / 2, pdfDoc.GetPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
canvasModel.SetFontColor(Color.GREEN, 0.2f);
canvas.EndLayer();
}
pdfDoc.Close();
This is what I have tried to remove the watermark. I want to remove it completely, not just set the layer to not display.(any sample code appreciated):
pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
IList<PdfLayer> layers = pdfDoc.GetCatalog().GetOCProperties(true).GetLayers();
for (var i = 0; i <= layers.Count; i++)
{
var t = layers[i].GetPdfObject().Get(PdfName.Name);
if (t.ToString().Equals("watermark"))
{
//Not what I want..need to remove the layer
layers[i].SetOn(false);
//This does not work...
//layers.RemoveAt(i);
}
}
pdfDoc.Close();
With help from the guys at iText I was able to solve this.
If you intend to remove the watermark later, you will need to add it as a 'PDF watermark annotation'.
To add a watermark on every page:
public void WatermarkPDF(string sourceFile, string destinationPath)
{
float watermarkTrimmingRectangleWidth = 300;
float watermarkTrimmingRectangleHeight = 300;
float formWidth = 300;
float formHeight = 300;
float formXOffset = 0;
float formYOffset = 0;
float xTranslation = 50;
float yTranslation = 25;
double rotationInRads = Math.PI / 3;
PdfFont font = PdfFontFactory.CreateFont(FontConstants.TIMES_ROMAN);
float fontSize = 50;
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
var numberOfPages = pdfDoc.GetNumberOfPages();
PdfPage page = null;
for (var i = 1; i <= numberOfPages; i++)
{
page = pdfDoc.GetPage(i);
Rectangle ps = page.GetPageSize();
//Center the annotation
float bottomLeftX = ps.GetWidth() / 2 - watermarkTrimmingRectangleWidth / 2;
float bottomLeftY = ps.GetHeight() / 2 - watermarkTrimmingRectangleHeight / 2;
Rectangle watermarkTrimmingRectangle = new Rectangle(bottomLeftX, bottomLeftY, watermarkTrimmingRectangleWidth, watermarkTrimmingRectangleHeight);
PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(watermarkTrimmingRectangle);
//Apply linear algebra rotation math
//Create identity matrix
AffineTransform transform = new AffineTransform();//No-args constructor creates the identity transform
//Apply translation
transform.Translate(xTranslation, yTranslation);
//Apply rotation
transform.Rotate(rotationInRads);
PdfFixedPrint fixedPrint = new PdfFixedPrint();
watermark.SetFixedPrint(fixedPrint);
//Create appearance
Rectangle formRectangle = new Rectangle(formXOffset, formYOffset, formWidth, formHeight);
//Observation: font XObject will be resized to fit inside the watermark rectangle
PdfFormXObject form = new PdfFormXObject(formRectangle);
PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.6f);
PdfCanvas canvas = new PdfCanvas(form, pdfDoc);
float[] transformValues = new float[6];
transform.GetMatrix(transformValues);
canvas.SaveState()
.BeginText().SetColor(Color.GRAY, true).SetExtGState(gs1)
.SetTextMatrix(transformValues[0], transformValues[1], transformValues[2], transformValues[3], transformValues[4], transformValues[5])
.SetFontAndSize(font, fontSize)
.ShowText("watermark text")
.EndText()
.RestoreState();
canvas.Release();
watermark.SetAppearance(PdfName.N, new PdfAnnotationAppearance(form.GetPdfObject()));
watermark.SetFlags(PdfAnnotation.PRINT);
page.AddAnnotation(watermark);
}
page?.Flush();
pdfDoc.Close();
}
To remove the watermark:
public void RemovetWatermarkPDF(string sourceFile, string destinationPath)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
var numberOfPages = pdfDoc.GetNumberOfPages();
for (var i = 1; i <= numberOfPages; i++)
{
// PdfAnnotation
PdfDictionary pageDict = pdfDoc.GetPage(i).GetPdfObject();
PdfArray annots = pageDict.GetAsArray(PdfName.Annots);
for (int j = 0; j < annots.Size(); j++)
{
PdfDictionary annotation = annots.GetAsDictionary(j);
if (PdfName.Watermark.Equals(annotation.GetAsName(PdfName.Subtype)))
{
annotation.Clear();
}
}
}
pdfDoc.Close();
}
What about variable length watermark text? How would you dynamically resize the rectangle to fit the text? This is not inbuilt into iText, you would need to play around with the following dimension parameters:
float watermarkTrimmingRectangleWidth = 600;
float watermarkTrimmingRectangleHeight = 600;
float formWidth = 600;
float formHeight = 600;
float formXOffset = -100;
float fontSize = 30;
For my use-case I checked the length of the watermark text and based on that adjusted the parameters accordingly eg:
if (watermarkText.Length <= 14)
{
watermarkTrimmingRectangleWidth = 200;
watermarkTrimmingRectangleHeight = 200;
formWidth = 200;
formHeight = 200;
formXOffset = 0;
fontSize = 30;
}
else if (watermarkText.Length <= 22)
{
watermarkTrimmingRectangleWidth = 300;
watermarkTrimmingRectangleHeight = 300;
formWidth = 300;
formHeight = 300;
formXOffset = 0;
fontSize = 30;
}
else if (...)
{
...
}
.
.
etc.
.
.
else if (watermarkText.Length <= 62)
{
watermarkTrimmingRectangleWidth = 600;
watermarkTrimmingRectangleHeight = 600;
formWidth = 600;
formHeight = 600;
formXOffset = -100;
fontSize = 20;
}
i have a working program where i can add an array to a Zedgraph and show this in a form.
Now i only want to change the display of the x-axis from points (0..400) to frequency (9e3..6e9 Hz).
Here are my currently working functions:
public void AddGraph(double[] Values, string LegendName)
{
int i = 0;
PointPairList list = new PointPairList();
for (i = 0; i < Values.Length; i++)
{
list.Add(i, Values[i]);
}
if (i > MaxXAxis)
MaxXAxis = i;
SList.Add(list);
SListColor.Add(Color.Black);
}
SListName.Add(LegendName);
}
public void ShowDiagram(string Title, string XAxisName, string YAxisName,
int Timeout_ms)
{
ZedGraph.ZedGraphControl zgc = new ZedGraphControl();
GraphPane myPane = zgc.GraphPane;
LineItem myCurve = null;
// Set the titles and axis labels
myPane.Title.Text = Title;
myPane.XAxis.Title.Text = XAxisName;
myPane.YAxis.Title.Text = YAxisName;
for (int i = 0; i < SList.Count(); i++)
{
myCurve = myPane.AddCurve(SListName[i], SList[i], SListColor[i],
SymbolType.None);
myCurve.Line.Width = 2;
}
// Add gridlines to the plot, and make them gray
myPane.XAxis.MinorGrid.IsVisible = true;
myPane.YAxis.MinorGrid.IsVisible = true;
myPane.XAxis.MinorGrid.Color = Color.LightGray;
myPane.YAxis.MinorGrid.Color = Color.LightGray;
myPane.XAxis.MinorGrid.DashOff = 0;
myPane.YAxis.MinorGrid.DashOff = 0;
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MajorGrid.Color = Color.Gray;
myPane.YAxis.MajorGrid.Color = Color.Gray;
myPane.XAxis.MajorGrid.DashOff = 0;
myPane.YAxis.MajorGrid.DashOff = 0;
// Move Legend to bottom
myPane.Legend.Position = LegendPos.Bottom;
zgc.AxisChange();
myPane.XAxis.Scale.Max = MaxXAxis;
zgc.Location = new Point(0, 0);
zgc.Size = new Size(panel_diagramm.ClientRectangle.Width, panel_diagramm.ClientRectangle.Height);
panel_diagramm.Controls.Add(zgc);
}
How can i change the above two functions that they display the frequency in the x-axis?
I already tried to change the AddGraph-function to pass the needed parameters and to calculate the list to have the correct values. But what then...?
public void AddGraph_Frequency(int **Points**, double **StartFrequency**,
double **StopFrequency**, double[] Values, string GraphColor, string LegendName)
{
...
double frequency = StartFrequency; //der erste Punkt
double Intervall = (StopFrequency - StartFrequency) / Points;
for (i = 0; i < Points; i++)
{
list.Add(frequency, Values[i]);
frequency = frequency + Intervall;
}
....
}
Thanks for any help
best regards
Solved.
Missing was:
myPane.XAxis.Scale.Max = Stopfrequency;
myPane.XAxis.Scale.Min = Startfrequency;
The output of my graph is really small, I can't really see the values at the x and y axis. Is there a way to change this, so the graph is bigger?
My code to output the graph is:
ZedGraphControl zc = new ZedGraphControl();
GraphPane pane = zc.GraphPane;
PointPairList list1 = new PointPairList();
LineItem curve1;
pane.Title.Text = title;
pane.XAxis.Title.Text = xAxisTitle;
pane.XAxis.Scale.Min = 0;
pane.XAxis.Scale.Max = 11;
pane.YAxis.Scale.Min = 1;
pane.YAxis.Scale.Max = 12;
pane.YAxis.Title.Text = yAXisTitle;
Int32 totalCount = ds.Tables[objectName].Rows.Count;
double[] xVals = new double[totalCount], yVals = new double[totalCount];
for (int i = 0; i < totalCount; i++)
{
xVals[i] = Convert.ToDouble(ds.Tables[objectName].Rows[i]["ntotal"]);
yVals[i] = Convert.ToDouble(ds.Tables[objectName].Rows[i]["isomonth"]);
}
list1.Add(xVals, yVals);
curve1 = pane.AddCurve("Temp curve", list1, Color.Green, SymbolType.Circle);
for (int i = 0; i < totalCount; i++)
{
TextObj t = new TextObj("Teest", curve1.Points[i].Y, curve1.Points[i].X);
t.FontSpec.Border.IsVisible = false;
pane.GraphObjList.Add(t);
}
curve1.Line.Width = 1.0F;
pane.GetImage().Save(outPutDestination, ImageFormat.Png);
pane.AxisChange();
GetImage() function gets the current size of the pane, so we just have to increase the size of the entire pane(i.e: dock & maximize the window & then use the method to get the image).
DockStyle currentStyle = zedGraphControl1.Dock;
var currentWindowState = this.WindowState;
zedGraphControl1.Dock = DockStyle.Fill;
this.WindowState = FormWindowState.Maximized;
zedGraphControl1.GetImage ().Save ( #"c:\Image_1.png" );
this.WindowState = currentWindowState;
zedGraphControl1.Dock = currentStyle;
I am creating text in a GDI+ GraphicsPath, using DrawString, and then outputting this to PDF as a path.
This is all working perfectly at the moment.
The time I have an issue is when the chosen font causes the outlines to overlap each other. I have an image example though being a new user i can't upload it... (seems pointless..?..)
I found a library and also someone who has achieved the same as what I am looking for in this blog
I have converted the code snippet to vb.net though I keep getting an empty solution from the library.
Has anybody else managed to pass in a graphicsPath containing a string and retrieve outlined text using this or a similar library?
Here's some C# code that works ...
using ClipperLib;
static public void PathToPolygon(GraphicsPath path, Polygons polys, Single scale)
{
GraphicsPathIterator pathIterator = new GraphicsPathIterator(path);
pathIterator.Rewind();
polys.Clear();
PointF[] points = new PointF[pathIterator.Count];
byte[] types = new byte[pathIterator.Count];
pathIterator.Enumerate(ref points, ref types);
int i = 0;
while (i < pathIterator.Count)
{
Polygon pg = new Polygon();
polys.Add(pg);
do {
IntPoint pt = new IntPoint((int)(points[i].X * scale), (int)(points[i].Y * scale));
pg.Add(pt);
i++;
}
while (i < pathIterator.Count && types[i] != 0);
}
}
static private PointF[] PolygonToPointFArray(Polygon pg, float scale)
{
PointF[] result = new PointF[pg.Count];
for (int i = 0; i < pg.Count; ++i)
{
result[i].X = (float)pg[i].X / scale;
result[i].Y = (float)pg[i].Y / scale;
}
return result;
}
private void DrawBitmap()
{
Font f = new Font("Arial", 90);
Pen myPen = new Pen(Color.FromArgb(196, 0xC3, 0xC9, 0xCF), (float)0.6);
SolidBrush myBrush = new SolidBrush(Color.FromArgb(127, 0xDD, 0xDD, 0xF0));
path.Reset();
Polygons polys;
path.AddString("ABC", f.FontFamily, (int)f.Style, f.Size, new Point(100, 100), null);
path.Flatten();
//scale all points up by 100 because Clipper uses integer coordinates
PathToPolygon(path, polys, 100);
path.Reset();
//offset polys remembering to multiply delta by scaling amount ...
polys = Clipper.OffsetPolygons(polys, 7 * 100, JoinType.jtRound);
for (int i = 0; i < polys.Count(); i++)
{
//reverses scaling ...
PointF[] pts2 = PolygonToPointFArray(polys[i], 100);
path.AddPolygon(pts2);
}
newgraphic.FillPath(myBrush, path);
newgraphic.DrawPath(myPen, path);
}